VBA find value in column

Magoosball

Board Regular
Joined
Jun 4, 2017
Messages
70
Office Version
  1. 365
I am trying to write a for loop that will try to find each value in sheet 1 column A in sheet 2 column A. If the value in sheet 1 column A exists anywhere in sheet 2 column A then do nothing. If the value doesn't exist in sheet 2 column A then set sheet 1 column D to "Does not exist".

This should continue until there is no more data in sheet 1 column A.

Thanks in advance!
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
try

Code:
Sub findon2()

Dim lr As Long
Dim lr2 As Long
Dim ws1 As Worksheet
Dim ws2 As Worksheet


Set ws1 = Sheets("Sheet1")
Set ws2 = Sheets("Sheet2")
lr = ws1.Cells(Rows.Count, "A").End(xlUp).Row
lr2 = ws2.Cells(Rows.Count, "A").End(xlUp).Row


'clear column D of any old "Does not exist"
ws1.Range("D2:D" & lr).ClearContents




For x = 2 To lr
    If Application.CountIf(ws2.Range("A2:A" & lr2), ws1.Cells(x, "A")) = 0 Then ws1.Cells(x, "D") = "Does not exist"
Next x




End Sub
 
Upvote 0
Solution
Why not just use a formula in Sheet1 column D ?

If a macro is really required, don't need a loop. Can enter formula and convert to values, or enter values directly with the Evaluate method.
 
Last edited:
Upvote 0
Code:
Sub v()
Dim lr1&, lr2&
lr1 = Cells(Rows.Count, 1).End(xlUp).Row
lr2 = Sheets("Sheet2").Cells(Rows.Count, 1).End(xlUp).Row
Range("D1:D" & lr1) = Evaluate("if(countif(Sheet2!A1:A" & lr2 & ",A1:A" & lr1 & ")=0,""Does not exist"","""")")
End Sub
 
Upvote 0
Code:
Sub v()
Dim lr1&, lr2&
lr1 = Cells(Rows.Count, 1).End(xlUp).Row
lr2 = Sheets("Sheet2").Cells(Rows.Count, 1).End(xlUp).Row
Range("D1:D" & lr1) = Evaluate("if(countif(Sheet2!A1:A" & lr2 & ",A1:A" & lr1 & ")=0,""Does not exist"","""")")
End Sub
Overall I was looking for VBA because this is part of a much larger script.
This worked really nicely as well. Thank you both!
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,186
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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