I'm trying to copy rows from one sheet to another worksheet based on a number of criteria (currently from a dropdown). I am currently able to do this for non-dynamic (non-changing) criteria using either an excel formula or VBA coda (which is much faster).
However, how can I set up my macro so that it captures the criteria selected in the drop down, rather than a pre-selected criteria in the code.
The drop down criteria is the following, where I would select start date, end date, product, and buy/sell. Preferably, I'd like the macro based on this dynamic selection which once run will produce the required data below.
Criteria (which will change as selected from dropdown)
2. VBA code
However, how can I set up my macro so that it captures the criteria selected in the drop down, rather than a pre-selected criteria in the code.
The drop down criteria is the following, where I would select start date, end date, product, and buy/sell. Preferably, I'd like the macro based on this dynamic selection which once run will produce the required data below.
Criteria (which will change as selected from dropdown)
- Excel Formula:
Excel Formula:
=IFERROR(INDEX(LNG_PORTFOLIO_2023_SG_HIST!$B$2:$AD$1000,SMALL(IF(COUNTIF(LNG_PORT_23_SG!$C$2,LNG_PORTFOLIO_2023_SG_HIST!$B$2:$B$1000)*COUNTIF(LNG_PORT_23_SG!$D$2,LNG_PORTFOLIO_2023_SG_HIST!$W$2:$W$1000)*COUNTIF(LNG_PORT_23_SG!$A$2,LNG_PORTFOLIO_2023_SG_HIST!$AC$2:$AC$1000)*COUNTIF(LNG_PORT_23_SG!$B$2,LNG_PORTFOLIO_2023_SG_HIST!$AD$2:$AD$1000),MATCH(ROW(LNG_PORTFOLIO_2023_SG_HIST!$C$2:$C$1000),ROW(LNG_PORTFOLIO_2023_SG_HIST!$C$2:$C$1000)),""),ROWS(LNG_PORTFOLIO_2023_SG_HIST!$A$1:B1)),COLUMNS(LNG_PORTFOLIO_2023_SG_HIST!$A$1:B1)),"")
2. VBA code
VBA Code:
Sub tgr()
Dim wsData As Worksheet
Dim wsDest As Worksheet
Dim aCriteria() As String
Set wsData = Sheets("LNG_PORTFOLIO_2023_SG_HIST") 'Copying FROM this worksheet (it contains your data)
Set wsDest = Sheets("LNG_PORT_23_SG") 'Copying TO this worksheet (it is your destination)
'Populate your array of values to filter for
ReDim aFruit(2 To 4)
aCriteria1(2) = "TTF M-1 Swap"
aCriteria2(4) = "Validated"
With wsData.Range("B2", wsData.Cells(wsData.Rows.Count, "B").End(xlUp))
.AutoFilter 1, aCriteria, xlFilterValues 'Filter using the array, this avoids having to do a loop
'Copy the filtered data (except the header row) and paste it as values
.Offset(1).EntireRow.Copy
wsDest.Cells(wsDest.Rows.Count, "A").End(xlUp).Offset(1).PasteSpecial xlPasteValues
Application.CutCopyMode = False 'Remove the CutCopy border
.AutoFilter 'Remove the filter
End With
End Sub