VBA VLOOKUP Formula Syntax Error

wsnyder

Board Regular
Joined
Sep 23, 2018
Messages
224
Office Version
  1. 365
Platform
  1. Windows
Hi all,

Using Excel 365.

I'm trying to build a VLookup Formula as a string in VBE so I can apply the formula to a Range using the Formula property of the Range Object.
I created a Function that returns a string
When in Break Mode, I hover on all of the variables, all fo the variables return expected values.

When I run the code, it returns an error:
Run-time error '438':
Object doesn't support this property or method
and highlights my line where I am creating the formula string

I've looked at this for a couple of hours now and can't figure out what is wrong.
Any thoughts?

Thanks
-w

VBA Code:
Private Function GetLookupFrmla(WhereFrmlaGoes As Range, _
                                LookupWhat As String, _
                                wb As Workbook, _
                                LookupArray As String, _
                                ReturnColNbr As Long) As String
                                            
    'Purpose    :   Get lookup frmla

    'Objects
        Dim ws As Worksheet
        
    'Initialize objects
        Set ws = wb.Worksheets("Pivot")
    
    'Get lookup frmla
        GetLookupFrmla = "=IFERROR(VLOOKUP(" _
                             & LookupWhat & "," _
                             & "'[" & wb.name & "]" _
                             & ws.Name & "'!" _
                             & LookupArray & "," _
                             & ReturnColNbr & "," _
                             & "FALSE),0)"
                
    'Tidy up
        Set ws = Nothing

End Function
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
That function looks ok - what's the line that is calling it?
 
Upvote 0
Two things to consider:
1. Do you have the referenced workbook open? From what I've been told, you can't do anything with an unopened workbook.
2. Try using a helper variable for your string, pass it into the variable, then return its value. Just a way to check.
 
Upvote 0

Forum statistics

Threads
1,223,447
Messages
6,172,206
Members
452,448
Latest member
Tupacandres

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