VBA Code Using Dictionary Search

Status
Not open for further replies.

Excelpromax123

Board Regular
Joined
Sep 2, 2021
Messages
167
Office Version
  1. 2010
Platform
  1. Windows
Hi guys. I have an excel file with about 60,000 lines, Currently using Vlookup as the file is extremely slow. Thanks to the forum for help with the dictionary code. Search like the image described below. sincerely thank
If using a formula, then at cell G5=IF(F5="","",IFERROR(VLOOKUP(F5,$B$5:$C$60000,2,0),0)) ( 60,000 lines very slow )

1661613245297.png
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Im surprised that the VLOOKUP is slow on 60000 rows. It shouldn't
Alternatively you could use a macro. Only runs when you want to. Keeps your file fast.

VBA Code:
Sub jec()
 Dim rng
 Set rng = Range("B5", Range("C" & Rows.Count).End(xlUp))
 With Range("F5", Range("F" & Rows.Count).End(xlUp))
   .Offset(, 1).Value = Evaluate("iferror(vlookup(t(if({1}," & .Address & "))," & rng.Address & ",2,0),0)")
 End With
End Sub
 
Upvote 0
Solution
Hi everyone,
here is a solution using Dictionary, as requested by Excelpromax123, adapted from my answer in another thread:
VBA Code:
Sub Test2()
    
    Dim Rng As Range, Dn As Range, n As Long, Dic As Object, ray As Variant
    
    Application.ScreenUpdating = False
    
    With Sheets(1)
        ray = .Range(.Range("B5"), .Range("C" & Rows.Count).End(xlUp))
    End With
    
    Set Dic = CreateObject("scripting.dictionary")
    
    Dic.CompareMode = 1
    
    For n = 1 To UBound(ray, 1)
        
        If Not Dic.exists(ray(n, 1)) Then Dic(ray(n, 1)) = (n)
        
    Next
    
    With Sheets(1)
        Set Rng = .Range(.Range("F5"), .Range("F" & Rows.Count).End(xlUp))
        
        For Each Dn In Rng
            
            If Dic.exists(Dn.Value) Then
                Dn.Offset(, 1) = ray(Dic(Dn.Value), 2)
            Else
                Dn.Offset(, 1) = 0
            End If
        Next Dn
    End With
    
    Application.ScreenUpdating = True
    
End Sub
 
Upvote 0
Im surprised that the VLOOKUP is slow on 60000 rows. It shouldn't
Alternatively you could use a macro. Only runs when you want to. Keeps your file fast.

VBA Code:
Sub jec()
 Dim rng
 Set rng = Range("B5", Range("C" & Rows.Count).End(xlUp))
 With Range("F5", Range("F" & Rows.Count).End(xlUp))
   .Offset(, 1).Value = Evaluate("iferror(vlookup(t(if({1}," & .Address & "))," & rng.Address & ",2,0),0)")
 End With
End Sub


Your code is too short and runs really fast. Thank you so much
In cell F5 I am using the formula below. Can you help me convert to a similar code?
F5 =SUMPRODUCT(ISNUMBER(SEARCH(""&$B$4:$B$12&"",""&E4&""))*$C$4:$C$12)



1661707213765.png
 
Upvote 0
In cell F5 I am using the formula below. Can you help me convert to a similar code?
F5 =SUMPRODUCT(ISNUMBER(SEARCH(""&$B$4:$B$12&"",""&E4&""))*$C$4:$C$12)

Duplicate to: Sum Vlookup each small value for 1 string

In future, please do not post the same question multiple times. Per Forum Rules (#12), posts of a duplicate nature will be locked or deleted.

In relation to your question here, I have closed this thread so please continue in the linked thread.
 
Upvote 0
Status
Not open for further replies.

Forum statistics

Threads
1,215,218
Messages
6,123,676
Members
449,116
Latest member
HypnoFant

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top