Vlookup Macro Help Please!

matt9man

New Member
Joined
Jan 4, 2011
Messages
22
I am newbie at vba and I am trying to get my vlookup using a macro to only return the NA# with the lookup value it used in another sheet. My goal then is too loop it so can do that through 1000's of rows. I first tried a "find" but my lookupvalues have //.. so "find" does not work but here is the vlookup macro I have:

Sub Macro2()
Dim Res As Variant

lookupval = Worksheets("Sheet1").Range("A1").Value
lookuprng = Worksheets("Sheet2").Range("A:B").Value

Res = Application.VLookup(lookupval, lookuprng, 1, False)
If IsError(Res) = False Then
Else

lookupval.Copy

Sheets("Compare Result").Range("A1").Select
ActiveSheet.Paste

End If

End Sub
 

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
what happens if you use vlookup with the same arguments but in Excel rather than VBA ? thanks

Kaps
 
Upvote 0
Welcome to the board...

Vlookup is overkill when the goal is just to verify the value exists in another column.
Try using MATCH for that purpose...

Also, the lookuprng needs to be a Range object, not a string or a value..

Try this

Code:
Sub Macro2()
Dim Res As Variant, lookupval As Variant
Dim lookuprng As Range

lookupval = Worksheets("Sheet1").Range("A1").Value
Set lookuprng = Worksheets("Sheet2").Range("A:A")

Res = Application.Match(lookupval, lookuprng, 0)
If IsError(Res) Then
    Sheets("Compare Result").Cells(Rows.Count, "A").End(xlup).Offset(1).Value = lookupval
End If
End Sub

Hope that helps.<!-- / message -->
 
Upvote 0
Hey thanks for the help... its now returning a subscript error on line

Sub Macro3()
Dim Res As Variant, lookupval As Variant
Dim lookuprng As Range
lookupval = Worksheets("Sheet1").Range("A1").Value
Set lookuprng = Worksheets("Sheet2").Range("A:A")
Res = Application.Match(lookupval, lookuprng, 0)
If IsError(Res) Then
Sheets("Compare Result").Cells(Rows.Count, "A").End(xlUp).Offset(1).Value = lookupval
End If
End Sub

Any ideas and also another thought is how to make it loop?

Thanks again
 
Upvote 0
do you get "Subscript Out Of Range" ?

That means the sheet in question was not found.
Check spelling and for extra spaces

"Hello" Vs " Hello" Vs "Hello "
 
Upvote 0
Try this to make it loop

Code:
Sub Macro2()
Dim Res As Variant, lookupval As Variant
Dim lookuprng As Range, c As Range
Dim LR As Long
LR = Sheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Row
Set lookuprng = Worksheets("Sheet2").Range("A:A")
For Each c In Sheets("Sheet1").Range("A1:A" & LR)
    lookupval = c.Value
    Res = Application.Match(lookupval, lookuprng, 0)
    If IsError(Res) Then
        Sheets("Compare Result").Cells(Rows.Count, "A").End(xlUp).Offset(1).Value = c.Value
    End If
Next c
End Sub
 
Upvote 0
Its working thanks a lot Jonmo!! I spent 9 hours on this yesterday :mad: next time I'll just ask for help. ...Thanks again!
 
Upvote 0
Two questions I first want to be able to copy that entire row that lookup value is associated in and copy where I currently have it going below. Second how do I input a cell 3 (col A) to the left of the copied cells which are in col D. The text that I want to be inputed is"ExtraActuals." Thanks

Sub ExtraActuals()

Dim Res As Variant, lookupval As Variant
Dim lookuprng As Range, c As Range
Dim LR As Long
LR = Sheets("Actual").Cells(Rows.Count, "A").End(xlUp).Row
Set lookuprng = Worksheets("Expected").Range("A3:A1500")
For Each c In Sheets("Actual").Range("A3:A" & LR)
lookupval = c.Value
Res = Application.Match(lookupval, lookuprng, 0)
If IsError(Res) Then
Sheets("Compare Result").Cells(Rows.Count, "D").End(xlUp).Offset(5).Value = c.Value
End If
Next c
End Sub

Thanks
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,219
Messages
6,123,678
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