Doing a Left VLOOKUP using VBA code?

Darth_Sullivan

New Member
Joined
Oct 23, 2013
Messages
48
I know it's possible to do a left lookup, I am currently using this formula on the worksheet:

=VLOOKUP($B4,CHOOSE({1,2},Sheet1!$C$2:$C$65536,Sheet1!$B$2:$B$65536),2,FALSE)

Unfortunately, I cannot use this. This spreadsheet has several userforms to populate my spreadsheet with data. When using this on the spreadsheet, anytime I add data using the userforms, excel freezes up.

My assumption is that i can attach this formula to a button click on a user form, therefore only returning the value when I want it. My problem comes when trying to translate what this code does to vba.

This is what I thought would give me what I wanted:
Code:
Private Sub CommandButton1_Click()

Dim top1 As Long
Dim top2 As Long
Dim top3 As Long
Dim top4 As Long
Dim top5 As Long

Dim name1 As String
Dim name2 As String
Dim name3 As String
Dim name4 As String
Dim name5 As String

Dim PTSrng As Range
Dim NAMErng As Range

Set PTSrng = Worksheets("Sheet1").Range("$C$2:$C$65536")
Set NAMErng = Worksheets("Sheet1").Range("$B$2:$B$65536")

top1 = WorksheetFunction.Large(PTSrng, 1)
top2 = WorksheetFunction.Large(PTSrng, 2)
top3 = WorksheetFunction.Large(PTSrng, 3)
top4 = WorksheetFunction.Large(PTSrng, 4)
top5 = WorksheetFunction.Large(PTSrng, 5)

name1 = WorksheetFunction.VLookup(top1, Choose(1, 2, PTSrng, NAMErng), 2, False)
name2 = WorksheetFunction.VLookup(top2, Choose(1, 2, PTSrng, NAMErng), 2, False)
name3 = WorksheetFunction.VLookup(top3, Choose(1, 2, PTSrng, NAMErng), 2, False)
name4 = WorksheetFunction.VLookup(top4, Choose(1, 2, PTSrng, NAMErng), 2, False)
name5 = WorksheetFunction.VLookup(top5, Choose(1, 2, PTSrng, NAMErng), 2, False)

Label1 = name1
Label2 = top1

Label3 = name2
Label4 = top2

Label5 = name3
Label6 = top3

Label7 = name4
Label8 = top4

Label9 = name5
Label10 = top5


End Sub

When I click, I'm get:

Run-time error '1004':
Unable to get the VLookup property of the WorksheetFunction class

I am very new to learning vba programming, which is why I have everything spaced out and spelled out with as much detail. That is just for me to try to understand what I am doing.

Thank you for your time.

tl;dr version: How to I translate the above formula to work in vba code?
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
The more traditional solution to VLOOKUP's Zoolander disability is to use Index/Match.

Try
=INDEX(Sheet1!$B$2:$B$65536,MATCH(B4,Sheet1!$C$2:$C$65536,0))
 
Upvote 0
The more traditional solution to VLOOKUP's Zoolander disability is to use Index/Match.

Try
=INDEX(Sheet1!$B$2:$B$65536,MATCH(B4,Sheet1!$C$2:$C$65536,0))

Thank you for the quick response.
How would I translate this for use in vba so I can attach it to a button click in a user form?
 
Upvote 0
In VBA I would do it like this
Code:
Dim x As Variant, lr As Long

With Sheets("Sheet1")
    lr = .Cells(Rows.Count,"C").End(xlup).Row
    x = Application.Match(Range("B4").Value, .Range("C1:C" & lr),0)
    If Not IsError(x) Then
        Label1 = .Cells(x, "B").Value
    End If
End With
 
Upvote 0

Forum statistics

Threads
1,223,237
Messages
6,170,928
Members
452,366
Latest member
TePunaBloke

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