VBA Error With Using A Named Range In A vlookup Formula

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
4,564
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Please consider this following simple code:

Rich (BB code):
Private Sub uf1cbx1_operatorini_Change()
    'populate uf1lbl1_name
    uf1lbl1_name = Application.WorksheetFunction.VLookup(uf1cbx1_operatorini.Value, ufrng1_staff, 2, False)
End Sub

When the user changes the value in the combobox (uf1cbx1_operatorini) on my form, it triggers an event to populate the label (uf1lbl1_name) within the same userform.

I am getting an error with the line in red.

uf1rng_staff is a named range referring to a range in the workbook, sheet2!O32:P42. The values available in my combobox are all listed in cells O32:O42.

Am I using this parameter wrong by referring to a named range?
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
What sort of values do you have in the combo? text or numbers?
 
Upvote 0
Ok, try
Code:
uf1lbl1_name = Application.WorksheetFunction.vlookup(uf1cbx1_operatorini.Value, Range("ufrng1_staff"), 2, False)
 
Upvote 0
If I replace the named range with "sheet2.range("O32:P42"), the vlookup works without error.
I should mention the error I get with using the named range is "Unable to get the VLookup property of the WorksheetFunction class."
 
Upvote 0
I substituted the erroneous line with :
Code:
uf1lbl1_name = Application.WorksheetFunction.VLookup(uf1cbx1_operatorini.Value, Range("ufrng1_staff"), 2, False)
as suggested. It now creates an "Method 'Range' of object '_Global' failed."
 
Upvote 0
You need to use Range(namedrange) (or [namedrange]) to refer to a named range in code.

Also you don't really need the lookup, you can use the ListIndex property of the combobox to get the row.
Code:
Private Sub uf1cbx1_operatorini_Change()
Dim idx As Long

    'populate uf1lbl1_name
    idx = uf1cbx1_operatorini.ListIndex

    If idx <> -1 then
        uf1lbl1_name.Value = Range("ufrng1_staff").Cells(idx+1, 2).Value
    End If

End Sub
 
Upvote 0
Thanks all.
There was a typo in the named range. All working wonderfully now.
Thanks Norie for sharing the ListIndex method with me!!
 
Upvote 0
Glad you sorted it & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,215,521
Messages
6,125,308
Members
449,218
Latest member
Excel Master

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