How to make this lookup macro run faster? alternatives?

hrithik

Active Member
Joined
Jul 26, 2010
Messages
336
The sheet "Main" has 104334 entries (lr) and sheet "Sheet1" has 26500 entries (LRb). The macro is an overkill, takes an eternity.
Is there any way to make it faster or possible alternatives...experts please help!

Code:
Sub mymac()
Dim j, i, lr, LRb As Long
Dim temp As Integer
Sheets("Main").Select
lr = ActiveSheet.UsedRange.Rows.Count
LRb = Range("'Sheet1'!" & "A" & Rows.Count).End(xlUp).Row
Application.ScreenUpdating = False
For i = 2 To lr
temp = WorksheetFunction.CountIf(Range("'Sheet1'!" & "A2:A" & LRb), Range("A" & i).Value)
If temp = 0 Then
Range("D" & i).Value = 0
Else
Range("D" & i).Value = 1
End If
Next
Application.ScreenUpdating = True
End Sub

Note: the functionality is to lookup Main sheet col A entries in Sheet1, and indicate a match by 1 or a no match by 0.
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Give this a shot:

Code:
Sub mymac()
Dim i   As Long, _
    LR  As Long, _
    LRb As Long, _
    x   As Variant
    
With Application
    .ScreenUpdating = False
    .EnableEvents = False
    .Calculation = xlCalculationManual
End With
With Sheets("Main")
    LR = .ActiveSheet.UsedRange.rows.Count
    LRb = Sheets("Sheet1").Range("A" & rows.Count).End(xlUp).row
    For i = 2 To LR
        Application.StatusBar = "Currently checking row " & i
        x = Application.Match(Range("A" & i).Value, Sheets("Sheet1").Range("A2:A" & LRb), 0)
        If IsError(x) Then
            Range("D" & i).Value = 0
        Else
            Range("D" & i).Value = 1
        End If
    Next
End With
With Application
    .ScreenUpdating = True
    .EnableEvents = True
    .Calculation = xlCalculationAutomatic
    .StatusBar = False
End With
End Sub
 
Upvote 0
With that amount of data, I don't think there will be a way to make it noticably faster.
 
Upvote 0
Have you tried just entering the formulas in the cells in D and then replacing them with their values afterwards?
 
Upvote 0
Try the less than 1 sec solution:
Rich (BB code):

Sub ExistanceOfMainInSheet1()
  Dim Rng As Range, t As Single, r As Long, a
  t = Timer
  
  ' Copy column A data of sheet "Sheet1" into array a()
  With Sheets("Sheet1")
    If .FilterMode Then .ShowAllData
    a = .Range("A2", .Cells(.Rows.Count, "A").End(xlUp)).Value
  End With
  
  ' Main processing
  With CreateObject("Scripting.Dictionary")
    .CompareMode = 1
    ' Create dictionary using data of Sheet1 Column A
    For r = 1 To UBound(a)
      .Item(a(r, 1)) = 0
    Next
    ' Copy column A data of sheet "Main" into array a()
    With Sheets("Main")
      Set Rng = .Range("A2", .Cells(.Rows.Count, "A").End(xlUp))
      a = Rng.Value
    End With
    ' Loop throuch a() and check existence of its values in the dictionary
    ' Put results of cheking into the same array a()
    For r = 1 To UBound(a)
      If .Exists(a(r, 1)) Then
        a(r, 1) = 1
      Else
        a(r, 1) = 0
      End If
    Next
  End With

  ' Copy a() to column D of sheet "Main"
  With Application
    .EnableEvents = False
    .ScreenUpdating = False
    ' Do offseting for column D and copy
    With Rng.Offset(, 3)
      .Value = a
    End With
    .EnableEvents = True
    .ScreenUpdating = True
  End With

  Debug.Print Round(Timer - t, 3)
End Sub
 
Last edited:
Upvote 0
Try the less than 1 sec solution:
Rich (BB code):
Sub ExistanceOfMainInSheet1()
 Dim Rng As Range, t As Single, r As Long, a
 t = Timer
 
 ' Copy column A data of sheet "Sheet1" into array a()
 With Sheets("Sheet1")
   If .FilterMode Then .ShowAllData
   a = .Range("A2", .Cells(.Rows.Count, "A").End(xlUp)).Value
 End With
 
 ' Main processing
 With CreateObject("Scripting.Dictionary")
   .CompareMode = 1
   ' Create dictionary using data of Sheet1 Column A
   For r = 1 To UBound(a)
     .Item(a(r, 1)) = 0
   Next
   ' Copy column A data of sheet "Main" into array a()
   With Sheets("Main")
     Set Rng = .Range("A2", .Cells(.Rows.Count, "A").End(xlUp))
     a = Rng.Value
   End With
   ' Loop throuch a() and check existense of its values in the dictionary
   ' Put results of cheking into the same array a()
   For r = 1 To UBound(a)
     If .Exists(a(r, 1)) Then
       a(r, 1) = 1
     Else
       a(r, 1) = 0
     End If
   Next
 End With
 ' Copy a() to column D of sheet "Main"
 With Application
   .EnableEvents = False
   .ScreenUpdating = False
   ' Do offseting for column D and copy
   With Rng.Offset(, 3)
     .Value = a
   End With
   .EnableEvents = True
   .ScreenUpdating = True
 End With
 Debug.Print Round(Timer - t, 3)
End Sub

Wow, I really need to learn how to use the Scripting.Dictionary object. Do you happen to have any links that go over its usage?
 
Upvote 0
Last edited:
Upvote 0

Forum statistics

Threads
1,224,590
Messages
6,179,758
Members
452,940
Latest member
rootytrip

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