Avoid 2nd loop in VBA

yxz152830

Active Member
Joined
Oct 6, 2021
Messages
393
Office Version
  1. 365
Platform
  1. Windows
Gurus,
i'm comparing 2 sets of data like below:

VBA Code:
Sub ajsidjaisdjiadj()
for each cell1 in xxx
for each cell2 in xxx
if cell1 = cell2 then
...
else
...
end if
next cell2
next cell1
End Sub

is there a way to avoid the 2nd loop by some kind of function?
like I can simply just do something like this:

VBA Code:
Sub ajsidjaisdjiadj()
for each cell1 in xxx

if cell1.value belongs to 2nd table then
...
else
...
end if
next cell1
End Sub
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Assum cell1 in range A1:C2
lookup range is F1:H2
VBA Code:
Sub test()
For Each cell1 In Range("A1:C2")
    If WorksheetFunction.CountIf(Range("F1:H2"), cell1) Then
        MsgBox "OK"
    End If
Next
End Sub
 
Upvote 0
Assum cell1 in range A1:C2
lookup range is F1:H2
VBA Code:
Sub test()
For Each cell1 In Range("A1:C2")
    If WorksheetFunction.CountIf(Range("F1:H2"), cell1) Then
        MsgBox "OK"
    End If
Next
End Sub
gosh this solves my what if condition is not equal to question. BTW though, if I change the condition to does not equal to , and if I want to use only loops, is there a solution to this?
 
Upvote 0
Like this?
VBA Code:
....
 If WorksheetFunction.CountIf(Range("F1:H2"), cell1)=0 Then
...
 
Upvote 0
Like this?
VBA Code:
....
 If WorksheetFunction.CountIf(Range("F1:H2"), cell1)=0 Then
...
I meant like not using any function. I kinda thought of a solution. Just trying to explore different options.
 
Last edited:
Upvote 0
You can try with "find method".
Im not at PC now, will come back soon.
I was trying to not use any function just loops, this is what I thought of:
VBA Code:
for each cell1 in table1
  for each cell2 in table2
if cell1=cell2 then
a=a+1
end if
next cell2
if a=0 then
...
end if
a = 0
next cell1

i think this can find out all cell1 that does not equal to any cell in table2 without using functions.
 
Upvote 0
With dictionary:

VBA Code:
Sub test()
  Dim dic As Object
  Dim c As Range
  Set dic = CreateObject("Scripting.Dictionary")
 
  For Each c In Range("B2:B10") 'table 2
    dic(c.Value) = Empty
  Next
 
  For Each c In Range("A2:A10") 'table 1
    If Not dic.exists(c.Value) Then
      c.Interior.Color = vbYellow
      'Or whatever you want to do if the cell in table 1 doesn't exist in table 2
    End If
  Next
End Sub
 
Upvote 0
Solution
With dictionary:

VBA Code:
Sub test()
  Dim dic As Object
  Dim c As Range
  Set dic = CreateObject("Scripting.Dictionary")
 
  For Each c In Range("B2:B10") 'table 2
    dic(c.Value) = Empty
  Next
 
  For Each c In Range("A2:A10") 'table 1
    If Not dic.exists(c.Value) Then
      c.Interior.Color = vbYellow
      'Or whatever you want to do if the cell in table 1 doesn't exist in table 2
    End If
  Next
End Sub
im a dictionary lover
 
Upvote 0

Forum statistics

Threads
1,214,870
Messages
6,122,019
Members
449,060
Latest member
LinusJE

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