Vlookup not working when used in userform

ijhoeq

Board Regular
Joined
Jun 20, 2018
Messages
61
Hello,

I am trying to use VLOOKUP in my userform but I keep getting the error "Run-time '1004': Unable to get the VLookup property of the WorksheetFunction class". This error occurs when I enter 'Case 0' in the code below. Can someone help me figure out why this won't work? Thanks!

Code:
Select Case ChargeType

Case 0
Worksheets("Liquid Control Sheet").Activate
Range("B8").Value = Range("B4").Value + 14.7 - Application.WorksheetFunction.VLookup(Range("B7"), Range("A32:B55"), 2, True)

Case 1
Worksheets("Other worksheet").Activate

Case 2
Worksheets("Something Else").Activate

End Select
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
I dont think you can put Application.WorksheetFunction within a Case.

I took out the Worksheets and range lines and ran them in a normal subroutine and they worked.
As soon as I added the Case it failed with the same error you're getting.
 
Upvote 0
Id have thought its more likely the vlookup is producing an error. You can use a vlookup within a case statement.
 
Upvote 0
Agreed but I didnt get an error when I tested with a value not present.
Even use FALSE at the end of the VLOOKUP.
 
Last edited:
Upvote 0
Thats weird, working now (??)

Even when the VLOOKUP fails ?
 
Last edited:
Upvote 0
Try this.
Code:
    Select Case ChargeType
        Case 0
            With Worksheets("Liquid Control Sheet")
                Res = Application.VLookup(.Range("B7").Value, .Range("A32:B55"), 2, True)
                If Not IsError(Res) Then
                    .Range("B8").Value = .Range("B4").Value + 14.7 - Res
                Else
                    MsgBox .Range("B7").Value & " not found in A32:A55 on Liquid Control Sheet"
                End If
            End With

        Case 1
            Worksheets("Other worksheet").Activate

        Case 2
            Worksheets("Something Else").Activate

    End Select
 
Upvote 0

Forum statistics

Threads
1,224,613
Messages
6,179,903
Members
452,948
Latest member
Dupuhini

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