VBA Vlookup function help please

pete393

New Member
Joined
Jan 23, 2005
Messages
21
Hello every body in my VBA code i have the folowing
xxxlist:
Name = InputBox("enter movie name")
If Name = Cancel Then
noanwser = MsgBox(" YOU DID NOT ENTER A MOVIE NAME DO YOU WISH TO EXIT THE PROGRAM", vbRetryCancel)
If noanwser = vbRetry Then GoTo xxxlist
If noanwser = vbCancel Then GoTo aend

moviedata = Application.WorksheetFunction. _
VLookup(Name, Range("movielistxxx"), 2, False)

now the problem im having is if the vlookup dosent find a corect anwser i get a run time error 1004 how could i make it now show this and even beter if i get this to execute an other function like a msgbox


can you please help this would be great
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Oh i forgot please dont think rong in my code its says XXX this is were the list name goes and not what you may think...


thanks
 
Upvote 0
Oh i forgot please dont think rong in my code its says XXX this is were the list name goes and not what you may think...


thanks
 
Upvote 0
Several ways. One is to add error handling:

Code:
Sub macro()
Dim Name As String, moviedata As Variant, noanswer As Variant

Name = InputBox("enter movie name")

    On Error Resume Next
    moviedata = Application.WorksheetFunction.VLookup(Name, Range("movielistxxx"), 2, False)
    If Err.Number <> 0 Then
        Err.Clear
        On Error GoTo 0
        MsgBox "Sorry, match not found"
    Else
        On Error GoTo 0
        MsgBox moviedata
    End If

End Sub
 
Upvote 0
Another way is to use countif first:

Code:
Sub macro()
Dim Name As String, moviedata As Variant

Name = InputBox("enter movie name")

    If Application.WorksheetFunction.CountIf(Range("movielistxxx"), Name) = 0 Then
        MsgBox "Sorry, match not found"
    Else
        moviedata = Application.WorksheetFunction.VLookup(Name, Range("movielistxxx"), 2, False)
        MsgBox moviedata
    End If

End Sub
 
Upvote 0
Thank you

Hello again just whanted to say thank alot this works great

you guys are great


:biggrin: :biggrin:
:pray: :pray:
 
Upvote 0

Forum statistics

Threads
1,213,535
Messages
6,114,198
Members
448,554
Latest member
Gleisner2

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