Wildcard reference to another workbook with INDIRECT Function - excel

I want to make a reference to another workbook , let's say its name is "My workbook.xlsm" with the INDIRECT Function in Excel.
However "my workbook" file name could have any other string/characters before it or after it.
let's say I'm Trying to lookup "Lookup Value"
=VLOOKUP(""&"Lookup Value"&"",INDIRECT("'["&""&"my workbook"&""&".xlsm"&"]"&"("&A7&")"&"'!$A:$H"),8,FALSE)
I've tried the above formula and when i remove the wildcards before and after "my workbook" reference and write the name of the workbook exactly as it is , the formula works fine, but when i place the wildcards the formula gives a REF Error.
I'm definitely doing something wrong.
can anyone help please?
thanks in advance.

A workbook referenced with INDIRECT() must be open for the reference to work. Therefore, this approach is a non-starter from the go, unless you happen to have all files that might potentially meet the wildcard criteria open at the same time with your first workbook.
You may want to have a look at the free add-in called MOREFUNC.XLL, which has a function INDIRECT.EXT() that works with closed workbooks.
Edit: there are no wildcards in your formula above. A wildcard can be a ? for a single character or a * for any number of characters. The wildcard usage for INDIRECT with sheet names is explained in the video in your comment. For your example the formula would look like
=VLOOKUP(A2,INDIRECT("'[*my workbook*.xlsx]Sheet1'!$A:$H"),8,0)
There is no need to split up a string into a series of concatenated strings, but if you do, it does not make a difference.
You will find that this formula will always return an error, though. Indirect cannot parse wildcards in file names. It works fine with sheet names.
As I said in my comment: Since the file with the lookup table needs to be open anyway, so INDIRECT() can work in the first place, you can use a regular Vlookup with a direct reference to "my workbook". If you rename that file, the Vlookup formula will reflect the change immediately.

Related

Extract the name of the workbook where the formula is saved and not the name of the workbook currently in use

I want to find an excel or Power query M (not vba) formula to extract the filename of the workbook where this formula is executed.
Internet seems to know only one :
=MID(CELL("filename"),SEARCH("[",CELL("filename"))+1, SEARCH("]",CELL("filename"))-SEARCH("[",CELL("filename"))-1)
unfortunately this formula provides only the name of the workbook currently in use,
so, if I use a second workbook before executing my function in my first workbook, the name given by this formula will be the name of my second file, which causes terrible mistakes.
Is there a more practical way of doing this ?
If you specify the second (reference) parameter of CELL, then the formula will be "locked" to the sheet/file in question:
=MID(CELL("filename",A1),SEARCH("[",CELL("filename",A1))+1, SEARCH("]",CELL("filename",A1))-SEARCH("[",CELL("filename",A1))-1)
This is discussed in the CELL docs as well as by Chip Pearson here.

Excel dynamic ranges from Access code and Formula separators

I am currently rewriting some old Access code that defined named ranges in an excel workbook.
The old code is as follow:
oWorkbook.Names.Add "NameOfRange", "='Sheet1'!$A$1:$L2000"
With oWorkbook a Workbook opened from Access.
Since the range (number of rows) can change, I planned to use dynamic named ranges using Offset and CountA functions, and wrote the following code to replace the previous one:
oWorkbook.Names.Add "NameOfRange", "=OFFSET('Sheet1'!$A$1,0,0,COUNTA('Sheet1'!$A:$A),12)"
but then excel send an error 1004 There is a problem with this formula.
The same line of code executed from an Excel workbook works fine, and give the expected result.
On my computer, I use mixed English/French international settings, and my Office 2016 is in English, so I have a ListSeparator as ;.
When replacing the , by ;, the new formula below works:
oWorkbook.Names.Add "NameOfRange", "=OFFSET('Sheet1'!$A$1;0;0;COUNTA('Sheet1'!$A:$A);12)"
but I need my function to work on every configuration, not on only French/English bizaroid settings.
I tried to specify the named arguments (Name:= ... and RefersTo:=...) but its not correcting the problem. When called from Access, the code still need local separators (and maybe local function names? no idea).
So I can retrieve the computer excel List Separator (Using oWorkbook.Application.International(xlListSeparator) and replace all , in the formula with it, but maybe I am doing something wrongly and there is a better way to do?

Reference a file that changes daily / Excel

Before I jump into a VBA solution, I wanted to know if it was possible to update a filename of a closed workbook I am referencing.
The formula reads,
=SUMIF('L:path\[filename - "&TEXT(WORKDAY(TODAY(),-1),"yyyymmdd")&".xlsx]Sheet'!$BF:$BF,"criteria",'L:path\[filename - "&TEXT(WORKDAY(TODAY(),-1),"yyyymmdd")&".xlsx]Sheet'!$DX:$DX)
To clarify, the path the filename would look something like this, This is the filename - yyyymmdd.xlsb
Someone at work said it's not possible to change the filename because it's a string. If that is the case could someone just expand on that a bit?
You can't really. If you have "Book1.xls" and "Book2.xls", what you can do it put this formula in book1:
=MID(CELL("filename",A1),FIND("[",CELL("filename",A1))+1,FIND("]", CELL("filename",A1))-FIND("[",CELL("filename",A1))-1)
This will return the name (only) of Book1. You could then reference that cell from Book2. HOwever, if you change Book1's name to something else, the reference will be broken and you will have to manually change the reference (pick the file from the update link manager). It seems excel uses absolute paths, including filename, to reference other workbook. You would need to have a way to store a flexible reference to that workbook into Book2. But the only way to do is VBA.
Interestingly however you can do that:
Create a reference in book2 pointing to a cell in book1. You get the value of that cell in book2.
Close everything.
Open only book1, edit the referenced cell. Close & save book1.
Book2, when you open it, will ask you if you want to update the link, and you can see the updated value even if book1 is still closed.
The other idea that I had was to use INDIRECT, like what is done there: using indirect function in Excel
But it won't work for closed workbook and that's one of your requirements.
So VBA it is.

Dynamically reference cell in different workbook

I have two workbooks:
WorkbookToUpdate.xls
Workbook_for_20130901.xls
In the first workbook I have the following:
A1 ='[Workbook_for_20130901]Sheet1'!$C5
Now a month goes by and I want to update the first work to reference Workbook_for_20131001.xls without going cell by cell and changing the name of the workbook. My thought was to make the date portion of the workbook name a variable and simply change that variable, but that doesn't seem to be working.
EDIT: I don't want to use Excel's INDIRECT function because I don't want to open the reference workbook.
I found one solution to be Harlan Grove’s PULL function (code can be found here), which works similarly to the INDIRECT function except that it doesn't require the source workbook to be open. The other solution, which actually works out to be faster than the Pull function (its only downfall) is the one I was using originally - Good ol' "find & replace". I thought that that was slow, but after trying the Pull function, it's not too bad.
Another option is by changing the source through excel's Data links, but this doesn't allow you to choose which cells keep the old source and which cells use the new one (in my case, I need the old values as well).

Excel dynamic hyperlink via formula

I am trying to create a hyperlink from one worksheet to another. The worksheet is large and there are many hyperlinks to places all over the other worksheet so I am trying to do this with a formula that I can copy/paste.
Here's what I have:
=HYPERLINK(CELL("address",INDEX('Test Results'!A:A,MATCH("TestCase-001",'Test Results'!A:A,0))),"TestCase-001")
The URI that it evaluates to is: '[Test Results 1.xls]Test Results'!$A$17, and A17 on the Test Results worksheet is indeed the correct cell to link to. The problem is when I click the hyperlink so I can just to test case details, I get an error saying "Cannot open the specified file", and I'm not sure why since the URI looks correct. Any ideas out there to help me fix this?
Using Excel 2003.
The CELL("address",... function returns in the format '[workbook]worksheet'!cell (as you say)
However, HYPERLINK expects [workbook]'worksheet'!cell (note the position of the ' marks) where worksheet name includes spaces.
Simplest answer would be to remove the spaces from the sheet names...
The CELL("address",... function returns in the format '[workbook]worksheet'!cell (as you say)
However, HYPERLINK expects [workbook]'worksheet'!cell (note the position of the ' marks) where worksheet name includes spaces.
Simplest answer would be to remove the spaces from the sheet names...
Figuring out why those two functions wouldn't work together was some great detective work!
Armed with that knowledge, I actually found a way to force the formatting from the response of the CELL function into the format that the HYPERLINK function expects/requires:
=HYPERLINK("[Workbook.xlsx]'Worksheet'!"&CELL("address", A10), "LinkLabel")
Without the full quotes, my version of office (2010) kept truncating the Workbook and Worksheet names because they were referring to the same sheet the code itself was being written in.

Resources