How to evaluate an workbook object by its name in code?

prabha_friend

Board Regular
Joined
Jun 28, 2011
Messages
95
Declaration:
Private wbAcctFees As Workbook

Public Function mac()
Set wbAcctFees = Workbooks.Open(frmPart1.txtAcctFees.Value)
End Function

Formula in a Cell:
=VLOOKUP(A2,INDIRECT("["&GetFileNamefromCode("wbAcctFees")&".xlsx]gen_rpt'!$A:$K"),11,)

Public Function GetFileNamefromCode(filevariable As String) As String
GetFileNamefromCode = Evaluate(filevariable).Name
End Function

Why its not working? and Whats the solution for this? Thanks in advance.
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Try removing the "".
Code:
GetFileNamefromCode(wbAcctFees)
 
Upvote 0
You need something like:

Code:
Private wbAcctFees As Workbook

Public Function mac()
Set wbAcctFees = Workbooks.Open(frmPart1.txtAcctFees.Value)
End Function
Public Function GetFileNamefromCode() As String
GetFileNamefromCode = wbAcctFees.Name
End Function
Formula in a Cell:
=VLOOKUP(A2,INDIRECT("["&GetFileNamefromCode()&".xlsx]gen_rpt'!$A:$K"),11,)

You can't evaluate a variable name like that. If you need to be able to use the function with different workbooks, you'd need to load them into a Collection or Dictionary.
 
Upvote 0
Yes - you can't evaluate a VBA variable that way.
 
Upvote 0
As I already said, you could load the workbooks into a Collection or Dictionary. You could also use defined names that you set when you open the workbooks. Without knowing what you are doing and why it's hard to say what your best option would be.

Edit: now cross-posted here, so good luck.
 
Last edited:
Upvote 0
Without knowing what you are doing and why it's hard to say what your best option would be.

Edit: now cross-posted here, so good luck.
I want to be able to pass in a variable name as a string (not the variable or its value) and then have the code take that variable name and then determine the value of that variable dynamically. Possible?
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

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