Alright so I just started using arrays with scripting dictionary because I'm trying to do some Index Match formulas on workbooks that are over 300,000 rows.
So I have the following code but I get an out of memory error sometimes when running it:
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim dic As Object
Dim arr() As Variant
Dim x As Long
Set ws1 = Sheets("Sheet1")
Set ws2 = Sheets("Sheet2")
Set dic = CreateObject("Scripting.Dictionary")
With ws2
x = .Cells(.Rows.Count, 1).End(xlUp).Row
arr = .Cells(1, 1).Resize(x, 7).Value
For x = LBound(arr, 1) To UBound(arr, 1)
dic(arr(x, 1)) = arr(x, 7)
Next x
End With
Erase arr
With ws1
x = .Cells(.Rows.Count, 1).End(xlUp).Row
arr = .Cells(1, 1).Resize(x, 7).Value
For x = LBound(arr, 1) To UBound(arr, 1)
If dic.exists(arr(x, 1)) Then
.Cells(x, 16).Value = dic(arr(x, 1))
End If
Next x
End With
Erase arr
Set dic = Nothing
Set ws1 = Nothing
Set ws2 = Nothing
So pretty much what the above code is saying is that my unique identifier is in column 1 of both worksheets which is what the code is looking up. I want to return column 7 from worksheet 2, and put the result in column 16 of worksheet 1.
The code works, but I get an out of memory error when running it on very large workbooks sometimes. I started using this code and my array was only the first 5 columns (x, 5), but it seems when I moved it to 7 columns (x, 7) that is when I'm getting the memory errors as each column has 300k+ rows. So I'm wondering if it's possible to adjust this array to only include my necessary columns (Columns 1 and Column 7) and leave out the other ones from the array?
Thanks.
Bookmarks