Hi all,
I am learning more about Arrays but I got to this point & I am stuck & need some help here. So in "Sheet1" I have a list of data that spreads for about 17k rows & over 9 columns & in "Sheet2" I have list of values that I type in column A which then I am looping against my data from Sheet1 & inserting the results in a new sheet. I have already searched & read a lot about arrays but I am still facing the below issues:
I am learning more about Arrays but I got to this point & I am stuck & need some help here. So in "Sheet1" I have a list of data that spreads for about 17k rows & over 9 columns & in "Sheet2" I have list of values that I type in column A which then I am looping against my data from Sheet1 & inserting the results in a new sheet. I have already searched & read a lot about arrays but I am still facing the below issues:
- I read that "ReDim Preserve" can only work with the last dimension of the array which I don't need to change in my case - I need to change the first dimension which is the rows
- How can I copy the data from Arr to Arr2 if the condition is met (all row data from 9 columns)
- I have read a post here few days ago that it's better not to use ReDim extensively in a loop & rather define a larger size Arr2 & then ReDim only once after completion - How could I accomplish this ?
This is my code so far, any guidance on how to complete it would be highly appreciate it
Rich (BB code):
Rich (BB code):
Rich (BB code):
Sub FilterData()
Dim Ws1 As Worksheet, Ws2 As Worksheet, Ws3 As Worksheet
Dim lRow As Long, Arr() As Variant, Arr2() As Variant
Set Ws1 = Sheet1 'All Data
Set Ws2 = Sheet2 'Look up list
Set Ws3 = Sheet3 'Sheets.Add(After:=Sheets(Sheets.Count))
Set Rg = Ws1.Range("A1").CurrentRegion
Arr() = Rg.Value ' For my file currently it's (1 to 17270, 1 to 9)
lRow = Ws2.Range("A" & Rows.Count).End(xlUp).Row
For x = 2 To lRow
For i = LBound(Arr) To UBound(Arr)
If Arr(i, 4) = Ws2.Cells(x, 1).Value Then
y = y + 1 'counting row numbers in Array
ReDim Preserve Arr2(1 To y, 1 To Rg.Columns.Count)
' How to assign data from Arr1 to Arr2 for all 9 columns ?
End If
Next i
Next x
Ws3.Range("A1").Resize(UBound(Arr2, 1), UBound(Arr2, 2)) = Arr2
End Sub