I have a VBA subroutine which filters records that have the text "SV-PCS7" in column 4. How can I get these results into an array?
Sub FilterTo1Criteria()
Dim xlbook As Excel.Workbook
Dim xlsheet As Excel.Worksheet
Dim ro As Integer
Set xlbook = GetObject("C:\07509\04-LB-06 MX-sv.xlsx")
Set xlsheet = xlbook.Sheets("04-LB-06 MX")
With xlsheet
.AutoFilterMode = False
.Range("blockn").AutoFilter Field:=1, Criteria1:="SV-PCS7"
End With
End Sub
If you want to avoid the complex looping of Jeeped's (excellent) solution, you can use a temp sheet to copy the visible rows first.
It looks like the best way to do it is looping through each row, checking to see if the row is hidden (
cell.EntireRow.Hidden = False
), and adding the data for that row into the array if it's not hidden. Similar example: Easiest way to loop through a filtered list with VBA?After applying the Range.AutoFilter Method and determining that there are visible cells, you need to work through the Range.Areas property of the Range.SpecialCells method with xlCellTypeVisible. Each of the areas will have one or more rows to process.
The Preserve option can be used with the ReDim statement but only the last range can be redimensioned. I've built the array in the wrong orientation and then used the TRANSPOSE function to flip the orientation. Note: there are limits to the amount of array elements that can be successfully flipped.