Structured Reference in UDF

nodemix2

New Member
Joined
Jun 29, 2022
Messages
2
Office Version
  1. 2021
Platform
  1. Windows
Hi,

I am trying to create a UDF for to shorten my formula used in my workbook.
The function does not work and returns a #Value error.
I am new to structured reference, but my research tells me that I can pass a formula as a string to Evaluate in VBA.
Please tell me what is going wrong?

VBA Code:
Function HoriVLUp(headerName As String) As Variant

Dim str As String
str = "VLOOKUP([@[Hori.]],HorizontalProfile[#All],MATCH(HorizontalProfile[[#Headers],[" & _
         headerName & _
        "]],HorizontalProfile[#Headers],0),FALSE)"
HoriVLUp = Application.Evaluate(str)

End Function
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Hi & welcome to MrExcel.

AFAIK you cannot use the structured reference for the lookup value, try it like
VBA Code:
Function HoriVLUp(headerName As String, Cl As Range) As Variant

Dim str As String
str = "VLOOKUP(" & Cl.Address & ",HorizontalProfile[#All],MATCH(HorizontalProfile[[#Headers],[" & _
         headerName & _
        "]],HorizontalProfile[#Headers],0),FALSE)"
HoriVLUp = Application.Evaluate(str)

End Function
and for the formula use =HoriVLUp("something",[@[Hori.]])
 
Upvote 0
Solution
Hi & welcome to MrExcel.

AFAIK you cannot use the structured reference for the lookup value, try it like
VBA Code:
Function HoriVLUp(headerName As String, Cl As Range) As Variant

Dim str As String
str = "VLOOKUP(" & Cl.Address & ",HorizontalProfile[#All],MATCH(HorizontalProfile[[#Headers],[" & _
         headerName & _
        "]],HorizontalProfile[#Headers],0),FALSE)"
HoriVLUp = Application.Evaluate(str)

End Function
and for the formula use =HoriVLUp("something",[@[Hori.]])
Works like a charm! Thank you.
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,838
Messages
6,121,885
Members
449,057
Latest member
Moo4247

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