Error 1004 Calling VLOOKUP from VBA

MuddyBrain3

New Member
Joined
Feb 21, 2011
Messages
44
I'm trying to call VLOOKUP from a VBA sub to return a Variant, n, from another worksheet. I've twisted and turned it any number of ways, but no luck. This is the latest coding:
Code:
n = Application.WorksheetFunction.VLookup("$H$9", Worksheets("Sales Record").Range("A1:ZZ1000"), counter , False)
Hope someone can give the correct syntax. Thanks.
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Hi,

The 1004 error is (probably) being caused by the search value not being found. In an actual worksheet formula this returns a #N/A error, in VBA it raises a 1004 error. Something like this:

Code:
Sub V()
    Dim n
    On Error GoTo IGotNothing
    n = Application.WorksheetFunction.VLookup(Range("H9"), Worksheets("Sheet1").Range("A1:B4"), 2, False)
    On Error GoTo 0
    msgbox(V)
 
 
    Exit Sub 
    IGotNothing:
    If Err.Number = 1004 Then
        n = CVErr(xlErrNA)
        Resume Next
    Else
        MsgBox ("Broke it good, didn't you?")
        End
    End If
End Sub

should do the trick.
 
Upvote 0
Thanks Chris. I also posted in another forum and have plenty to consider! I'll need time to ponder all the information. Appreciate the suggestion. Muddy Brain is where I am right now so have to leave this for a while...
 
Upvote 0

Forum statistics

Threads
1,224,525
Messages
6,179,317
Members
452,905
Latest member
deadwings

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