HLOOKUP error using named range in VBA

Inovo

New Member
Joined
Dec 7, 2016
Messages
3
I have an HLOOKUP function that works in a spread sheet referencing a named range, but I cannot get it to work in a VBA Macro.

In the spread sheet, the function is as follows:
Code:
=HLOOKUP(DATEVALUE("12/12/16"),JN.10634, 2, FALSE)

JN.10634 is the named range, and the lookup works, finding the date and returning the value on the row below.

In my vba macro, I am assigning a cell value with the following statement:

Code:
.Cells(8, 12).Value = WorksheetFunction.HLookup(DateValue(Date28), fixedName, 2, False)

Date28 is a variable assigned the value 12/12/16

The code is failing on the variable fixedName and giving the error "Unable to get the HLOOKUP property of the WorksheetFunction class".

During debug mode with the cursor on the fixedName variable it shows the following:
fixedName = "JN.10634"

Any ideas why is knows the variable resolves to JN.10634 (which is the name of a named range) but it generates an error?
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
It is not because of the named range - it is because (as the error says) HLOOKUP is not available to to WorksheetFunction. You can demonstrate this in the immediate window of the VBA editor by typing

WorksheetFunction.

and then having a look through the resultant list that pops up.

To fix the problem, put the HLOOKUP into the cell as a formula, and then convert it to a value, like so

Code:
Option Explicit
Sub test()
    Dim Date28 As String, fixedName As String
    Date28 = "12/12/2016"
    fixedName = "JN.10634"
    With Cells(8, 12)
        .Formula = "=HLookup(DateValue(""" & Date28 & """), " & fixedName & ", 2, False)"
        .Value = .Value
    End With
End Sub
Regards

Murray
 
Upvote 0
How exactly have you declared and set fixedName?

PS HLookUp is available via WorksheetFunction.
 
Upvote 0

Forum statistics

Threads
1,202,984
Messages
6,052,910
Members
444,612
Latest member
FajnaAli

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