timmytonga
New Member
- Joined
- Sep 23, 2011
- Messages
- 21
Hi,
Vlookups were taking forever so I wrote the below code to use the scripting dictionary. It works fine on smaller amounts of data but I am getting the Run time error on my "live run". I assume it is due to the size of my data. how can I fix this. My work sheets are between 250k and 350k+ rows
I noted where the error happens. hovering over "vlookuptable" 3 lines above the error I can tell it is a memory issue.
Not quite sure how to run the code so that it would loop at say 50,000 line increments
Please and thank you
Module
FORM CODE
Vlookups were taking forever so I wrote the below code to use the scripting dictionary. It works fine on smaller amounts of data but I am getting the Run time error on my "live run". I assume it is due to the size of my data. how can I fix this. My work sheets are between 250k and 350k+ rows
I noted where the error happens. hovering over "vlookuptable" 3 lines above the error I can tell it is a memory issue.
Not quite sure how to run the code so that it would loop at say 50,000 line increments
Please and thank you
Module
Code:
[INDENT=2]Public LookupFromwb As String
Public ReturnTowb As String
Sub FAST_VLOOKUP()
Dim dicLookupTable As Scripting.Dictionary
Dim i As Long
Dim sKey As String
Dim vLookupValues As Variant
Dim vLookupTable As Variant
Set dicLookupTable = New Scripting.Dictionary
dicLookupTable.CompareMode = vbTextCompare
WBsel.Show
Dim myRefValues As Range
Dim myResults As Range
Dim MyLookValues As Range
Dim MyresultValues As Range
Workbooks(ReturnTowb).Activate
Set myRefValues = Application.InputBox("Please select the first cell in the column with the reference values that are using for the lookup.", Type:=8)
Dim MRV As String
MRV = myRefValues.Parent.Name
Workbooks(ReturnTowb).Worksheets(MRV).Activate
Set myResults = Application.InputBox("Please select the first cell where you want your lookup results to start ", Type:=8)
Dim MR As String
MR = myResults.Address(External:=False)
Workbooks(LookupFromwb).Activate
Set MyLookValues = Application.InputBox("Please select the first cell where your reference values start ", Type:=8)
Dim MLV As String
MLV = MyLookValues.Parent.Name
Workbooks(LookupFromwb).Worksheets(MLV).Activate
Set MyresultValues = Application.InputBox("Please select the first cell where the values we are returning start ", Type:=8)
Dim finalrow As Long
[/INDENT]
[INDENT=2]Dim finalrow2 As Long[/INDENT]
[INDENT=2]Dim Cntr As Integer[/INDENT]
[INDENT=2]
[/INDENT]
[INDENT=2]
[/INDENT]
[INDENT=2] Cntr = (MyresultValues.Column - MyLookValues.Column + 1)[/INDENT]
[INDENT=2]
[/INDENT]
[INDENT=2]Workbooks(LookupFromwb).Activate[/INDENT]
[INDENT=2]
[/INDENT]
[INDENT=2]With Worksheets(MLV)[/INDENT]
[INDENT=2] finalrow = MyLookValues.SpecialCells(xlCellTypeLastCell).Row[/INDENT]
[INDENT=2] vLookupTable = .Range(.Cells(MyLookValues.Row, MyLookValues.Column), .Cells(finalrow, MyresultValues.Column))[/INDENT]
[INDENT=2] For i = LBound(vLookupTable) To UBound(vLookupTable)[/INDENT]
[INDENT=2][COLOR=#ff0000] sKey = vLookupTable(i, 1) '<--------------------------------- [B] Error kicks out here[/B][/COLOR][COLOR=#ff0000][/COLOR][/INDENT]
[INDENT=2] If Not dicLookupTable.Exists(sKey) Then _[/INDENT]
[INDENT=2] dicLookupTable(sKey) = vLookupTable(i, Cntr)[/INDENT]
[INDENT=2] Next i[/INDENT]
[INDENT=2] [/INDENT]
[INDENT=2] Workbooks(ReturnTowb).Activate[/INDENT]
[INDENT=2] [/INDENT]
[INDENT=2] With Worksheets(MRV)[/INDENT]
[INDENT=2] finalrow2 = myResults.SpecialCells(xlCellTypeLastCell).Row[/INDENT]
[INDENT=2] vLookupValues = .Range(.Cells(myRefValues.Row, myRefValues.Column), .Cells(finalrow2, myRefValues.Column))[/INDENT]
[INDENT=2] [/INDENT]
[INDENT=2] For i = LBound(vLookupValues) To UBound(vLookupValues)[/INDENT]
[INDENT=2] sKey = vLookupValues(i, 1)[/INDENT]
[INDENT=2]
[/INDENT]
[INDENT=2] If dicLookupTable.Exists(sKey) Then[/INDENT]
[INDENT=2] vLookupValues(i, 1) = dicLookupTable(sKey)[/INDENT]
[INDENT=2] Else[/INDENT]
[INDENT=2] vLookupValues(i, 1) = CVErr(xlErrNA)[/INDENT]
[INDENT=2] End If[/INDENT]
[INDENT=2] Next i[/INDENT]
[INDENT=2] [/INDENT]
[INDENT=2] .Range(MR).Resize(UBound(vLookupValues) - LBound(vLookupValues) + 1, 1) = vLookupValues[/INDENT]
[INDENT=2] [/INDENT]
[INDENT=2] End With[/INDENT]
[INDENT=2]End With[/INDENT]
[INDENT=2]
[/INDENT]
[INDENT=2]
[/INDENT]
[INDENT=2]Application.ScreenUpdating = True[/INDENT]
[INDENT=2]Application.EnableEvents = True[/INDENT]
[INDENT=2]
[/INDENT]
[INDENT=2]End Sub[/INDENT]
FORM CODE
Code:
Private Sub UserForm_Initialize()
Dim wb As Workbook
'Get the name of all the workbooks in the combobox
For Each wb In Application.Workbooks
LookupFrom.AddItem wb.Name
LookupTo.AddItem wb.Name
Next
LookupFrom = ActiveWorkbook.Name
End Sub
Private Sub CommandButton1_Click()
LookupFromwb = Me.LookupFrom
ReturnTowb = Me.LookupTo
Unload Me
End Sub