VLOOKUP Macro Workbook Reference Variable

crobinson661

New Member
Joined
Nov 15, 2019
Messages
11
Good morning,

I am trying to create a macro that opens reference files and later will pull data out to compare in primary workbook (wkb).

wkb3 is going to be the previous days file in each case, and opening the file works without issue.

I am running into an issue referencing the Workbook variable wkb3 in the IFNA(VLOOKUP formula despite the fact that the workbook is already open.

I have googled tirelessly and have attempted the following syntax:

"=IFNA(VLOOKUP(RC[-5],'[" & wkb3 & "]'!C5:C9,5,0),"""")"
"=IFNA(VLOOKUP(RC[-5],[" & wkb3 & "]!C5:C9,5,0),"""")"
"=IFNA(VLOOKUP(RC[-5],'wkb3'!C5:C9,5,0),"""")"
"=IFNA(VLOOKUP(RC[-5]," & wkb3 & "!C5:C9,5,0),"""")"
"=IFNA(VLOOKUP(RC[-5],["wkb3"]!C5:C9,5,0),"""")"
"=IFNA(VLOOKUP(RC[-5],wkb3!C5:C9,5,0),"""")"

The first syntax is a match for another thread I found here:
http://www.vbaexpress.com/forum/archive/index.php/t-58472.html

but I get a Run-Time error 438: Object doesnt support this property or method

Any help would be appreciated.


Code:
Sub Product_Valuation()
'
' Product_Valuation Macro
' Product Valuation
'


'
    FileDate = Format(Date - 1, "DD-MMM-YYYY")
    FilePath = "V:\ParentDirectory\Folder1\Folder2\Folder3\Folder4 " & FileDate & ".csv"
    
    Dim wkb As Workbook
    Set wkb = ActiveWorkbook
    
    Dim wkb2 As Workbook
    Set wkb2 = Workbooks.Open("V:\ParentDirectory\Folder1\Folder2\ProductPriceFile.xlsx")
    
    Dim wkb3 As Workbook
    Set wkb3 = Workbooks.Open(FilePath)


    wkb.Activate
    
    Columns("I:R").Select
    Selection.Delete Shift:=xlToLeft
    Columns("E:E").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    Range("E2").Select
    Application.CutCopyMode = False
    ActiveCell.FormulaR1C1 = "=UPPER(RC[-1])"
Selection.AutoFill Destination:=Range("E2:E" & Range("A" & Rows.Count).End(xlUp).Row)
Range(Selection, Selection.End(xlDown)).Select
    Columns("E:E").Select
    Selection.Copy
    Columns("F:F").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Columns("E:E").Select
    Application.CutCopyMode = False
    Selection.Delete Shift:=xlToLeft
    Range("E1").Select
    ActiveCell.FormulaR1C1 = "Upper"
    Range("E3").Select
    
    Range("J2").Select
    Application.CutCopyMode = False
    Application.CutCopyMode = False


    Range("J2").FormulaR1C1 = _
        "=IFNA(VLOOKUP(RC[-5],[" & wkb3 & "]!C5:C9,5,0),"""")"
    Range("J2").Select
    Selection.AutoFill Destination:=Range("J2:J80")
    Range("J2:J80").Select
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Since the variable is a Workbook object, you need to use its Name property to write that into the formula:

Code:
"=IFNA(VLOOKUP(RC[-5],'[" & wkb3.Name & "]'!C5:C9,5,0),"""")"
 
Upvote 0
Hate to bump, but I'm stuck.....
I posted Friday, but with 56 views, I'm the only post without a reply through Sunday, so I'll try to parse my inquiry.

VBA Code:
Range("J2").FormulaR1C1 = _
        "=IFNA(VLOOKUP(RC[-5],[" & wkb3 & "]!C5:C9,5,0),"""")"
    Range("J2").Select
    Selection.AutoFill Destination:=Range("J2:J80")
    Range("J2:J80").Select

This IFNA(VLOOKUP attempts to reference another workbook opened earlier in the Macro. I think my syntax is wrong, I've tried several variations and cannot figure out how it's wrong.

Most recently, I am getting "Run Time error 438: Object doesn't support this property or method"

Here's the code that opens the file:

VBA Code:
FileDate = Format(Date - 1, "DD-MMM-YYYY")
    FilePath = "V:\ParentDirectory\Folder1\Folder2\Folder3\Folder4 " & FileDate & ".csv"
    
    Dim wkb As Workbook
    Set wkb = ActiveWorkbook
    
    Dim wkb2 As Workbook
    Set wkb2 = Workbooks.Open("V:\ParentDirectory\Folder1\Folder2\ProductPriceFile.xlsx")
    
    Dim wkb3 As Workbook
    Set wkb3 = Workbooks.Open(FilePath)

Thanks for any suggestions
 
Upvote 0
Thanks for the reply Rory; however I am now getting "Run-Time error '1004': Application-defined or object-defined error"?

Here is the specfic referencing code after adjustment:

VBA Code:
FileDate = Format(Date - 3, "DD-MMM-YYYY")
    FilePath = "V:\COOBusinessOffice\Software Asset Management\2019 Robinson\Adobe Daily Output\Adobe Daily " & FileDate & ".csv"
    
    Dim wkb As Workbook
    Set wkb = ActiveWorkbook
    
    Dim wkb2 As Workbook
    Set wkb2 = Workbooks.Open("V:\COOBusinessOffice\Software Asset Management\2019 Robinson\Adobe Per Product Expense List.xlsx")
    
    Dim wkb3 As Workbook
    Set wkb3 = Workbooks.Open(FilePath)

VBA Code:
Range("J2").FormulaR1C1 = _
        "=IFNA(VLOOKUP(RC[-5],'[" & wkb3.Name & "]'!C5:C9,5,0),"""")"
    Range("J2").Select
    Selection.AutoFill Destination:=Range("J2:J80")
    Range("J2:J80").Select


Since the variable is a Workbook object, you need to use its Name property to write that into the formula:

Code:
"=IFNA(VLOOKUP(RC[-5],'[" & wkb3.Name & "]'!C5:C9,5,0),"""")"
 
Upvote 0
Oops - you're missing a worksheet name in there.

Code:
"=IFNA(VLOOKUP(RC[-5],'[" & wkb3.Name & "]Folder4 " & FileDate & "'!C5:C9,5,0),"""")"
 
Upvote 0

Forum statistics

Threads
1,224,813
Messages
6,181,118
Members
453,021
Latest member
Justyna P

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