简体   繁体   中英

Vlookup Function - Run-Time Error '1004'

I keep getting this error when I am running my code and I am unsure where else to look.

The Error message Reads: Run-Time Error '1004' - Unable to get the Vlookup Property of the WorksheetFunction Class

Here is my code, varMajor is declared as a global variable in another worksheet. Any help would be greatly appreciated!

Private Sub cmdRank_Click()
    Dim strMajorRank As String
    'Declare worksheet as an object variable
    Dim shtRankings As Worksheet
    'Set the variable
    Set shtRankings = Application.Workbooks("MGMT 3210 Final Project.xlsm").Worksheets("Rankings")
    varMajor = InputBox("Please Enter Your Major", "Where Does Your Major Rank?", vbOKOnly)
    'Set rngMajorRank = Application.Workbooks("MGMT 3210 Final Project.xlsm").Worksheets("Rankings").Range("H2")

    'Check to see if the major entered is listed in the Majors column in the data on the Rankings Worksheet using VLookup
    If varMajor = "Finance" Then
        strMajorRank = Application.WorksheetFunction.VLookup(varMajor, shtRankings.Range("B2:B21"), 1, True)
        MsgBox "Your major is among the 20 most popular at CU!"

        ElseIf varMajor = "Management" Then
        strMajorRank = Application.WorksheetFunction.VLookup(varMajor, shtRankings.Range("B2:B21"), 1, True)
        MsgBox "Your major is among the 20 most popular at CU!"

        ElseIf varMajor = "Marketing" Then
        strMajorRank = Application.WorksheetFunction.VLookup(varMajor, shtRankings.Range("B2:B21"), 1, True)
        MsgBox "Your major is among the 20 most popular at CU!"
    Else
        MsgBox "Your major is still a good one to have!"
    End If
    'Go to worksheet with top 20 majors at CU Application.Workbooks("MGMT 3210 Final Project.xlsm").Worksheets("Rankings").Activate 

shtRankings.Range("H2").Value = "strMajorRank"

End Sub

So it looks like your syntax is correct, this error actually just means that the Vlookup is not returning a value, ie in Excel this Vlookup would return N/A#, you can handle this with error handling. If the formula is correct you'd want to put "On Error Resume Next" and "On Error Goto 0" around your uses of Vlookup.

Your code's a bit unclear, assuming you're trying to determine if the value the user enters is in the list as you say, then it should be:

'Check to see if the major entered is listed in the...
On Error Resume Next 'Goes to next line of code on error
    strMajorRank = Application.WorksheetFunction.VLookup(varMajor, shtRankings.Range("B2:B21"), 1, True)
On Error Goto 0 'Returns error handling to default

If strMajorRank <> "" Then
    MsgBox "Your major is among the 20 most popular at CU!"
Else
    MsgBox "Your major is still a good one to have!"
End If
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM