Reference Excel files getting filename from a cell - excel

I want to get values from different excel files (say, File-AAA.xlsx, File-ABC.xlsx, etc). ¿How can I reference said files without inputting manually the "ABC" part of the filename, but getting it from the current sheet instead?
In the example, from our current sheet we want to get the values of A1 and A2, from the File-ABC.xlsx file, and sum them.
For instance, in B2, instead of writing:
=SUM('\\server\path\[File-ABC.xlsx]Data'!A1:A2)
I would like to be able to write it as
=SUM('\\server\path\[File-*REFERENCE TO CELL*.xlsx]Data'!A1:A2)
Where *REFERENCE TO CELL* is the sought text string that references the cell that contains "ABC" (A2).

You can use the indirect function for this.
Something like this -
=SUM(INDIRECT("'\\server\path\[File-"&A2&".xlsx]Data'!A1"&":"&"A2"))
Edit-1 :
You can use the same syntax to refer to the local directory that you want, just replace \server\path[File- with the path of the new directory. You can copy the path from your file explorer after browsing to the concerned folder.
Eg. Taking the path from your comment (after correction):
=SUM(INDIRECT("'C:\Users\PeteThePanda\Spreadsheets[File-"&A3&".xlsx]Data'!A1"&‌​":"&"A2"))

A more straightforward way to do this is set up a conditional statement outside the sum, e.g. if the value of A2 in the current workbook is ABC, then go to this path and bring in the sum.
e.g.
=if(A2="ABC",sum('\\server\path\[File-ABC.xlsx]Data'!A1:A2),if(A2="AAA", sum('\\server\path\[File-AAA.xlsx]Data'!A1:A2),"No File"))
Note, you can add additional nested IFs if there are more files. Just start a new if where I wrote "No File".

Related

Get file path from excels cell

I need help how to get the file path from excel woorbook.
Path is save it in Cell C3 in excel sheet1.
And I want it with my macro code to get that particular value from Cell C3 that I have in excel and set it to S1.
example:
Now my code work like this:
' Set s1 = Workbooks.Open(Filename:="C:\Users\25012023\master_NEW.xlsx")
But I don't wanna to fix that path name here in the module.
I wanted to be more dynamically.
I save it that path in excel cell , but now I don't know how to get the value from there.
I tried with this?
Set s1 = ThisWorkbook.Worksheets("Sheet1").Range("C3").Value
But without any success.
Suggestions?
Not sure if this is what you are looking for, but this formula will produce a dynamic file path/name in Excel. Simply enter this formula into cell A1 (substitute that cell address for another of your choosing:
=LEFT(CELL("filename",$A$1),FIND("[",CELL("filename",$A$1),1)-1)
I use this frequently when I need a dynamic way of getting the file name and path for Power Query for files that are on SharePoint.

how to fix Excel VLookup return #ref! with Indirect + concatenation function to build a file path

I try to isolate each part of my excel function and can't find the solution for now.
I'm using in a same row 3 functions
Vlookup (search value from a specific column based on a reference/ID)
Concatenate (building a file path from a tab which can be dynamic)
Indirect (turning the concatenation into a text input)
I've a file located in my computer such as
Path:desktop/folder A/
File names: data_fr.xls (for French and a second file data_en.xls for English product data in French or English but same product ID).
content on the data_fr an an exemple
Column A
Column B
row 1
1
John
row 2
2
Tintin
Those path and file names are saved in a separate excel file named test.xls and tab called "dynamic".
language
Column A
Column B
French
desktop/folder A/
data_fr.xls
English
desktop/folder A/
data_en.xls
from my new tab called "trial" of the test.xls excel file I want to collect the information from the file data_fr, tab content, data from product ID 1 (as an exemple).
if the vlookup works like a charm with the direct path, it is not working as soon as I want to create the path using indirect function
=vlookup(1,'desktop/folder A/[data_fr.xls]content'!$A:$B,2,false)
return:John
but by doing this it does not work and I don't know why.
=vlookup(A1,indirect(concatenate("'",'dynamic'!B$2,"[",'dynamic'!C$2,"]content'!$A:$B")),1,false)
return: #ref!
I've isolated the concatenation function and it works but as soon as I'm adding the indirect function it is broken. Some things is missing.
What do you have in mind which could help me?
FYI: I'm using a Mac, and the files are both opens.
I try the basic vlookup and works, isolated the concatenate function to build the path and the path looks accurate.
As soon as I'm adding the indirect function, it returns an error and don't know why.
Regards,
Henri
It is now working today and to be honest I don't know very much what happened.
Two links helped me to solve my issue
https://www.excelforum.com/excel-formulas-and-functions/1110218-file-path-concatenate.html
https://exceljet.net/functions/indirect-function
I rebuilt this morning anyway my function
with file Path , file name, concatenate Path & file, indirect and to finish vlookup.
VLOOKUP("datatosearch",INDIRECT(CONCATENATE("'",F4,"[",G4,"]Generic tab'!$B2:$AC14")),4,FALSE)
Job done.
Thanks

Change excel filename from excel

Is it possible to change excel filename from a specific excel file?
For example, create a cell that will store filename, which will depend on some other cell. If I change original cell, it should rename some third file, and set its name to a cell's content.
I don't want to use any other language, to create a script or something similar.
A formula in a cell cannot change the name of a file, regardless of whether it's the current file or a different file. To achieve what you describe you will definitely need "a script or something similar".
Don't shoot the messenger. This answers your question as stated.

Dynamically referencing a cell

I would like to set up Names for the drive, file path, Excel spreadsheet name and sheet name in cells in Excel, and then concatenate them together when referencing external cells. This will allow me to create macros that reference sheets dynamically in the sense that it does not matter where the spreadsheet is.
So far, I have the following:
Drive = C
Location = Users\ashleys\Desktop\New Quote Production\Quote Location 3\Ash
Name = Book3
Sheet = Sheet1
(Please note that the = sign above can be read as "assigned Name as")
I concatenate these together to create another Name (called Final) in the following fashion:
Final = "'"&Drive&":\"&Location&"["&Name&".xlsx]"&Sheet&"'!"
Final resolves to be the following:
C:\Users\ashleys\Desktop\New Quote Production\Quote Location 3\Ash\[Book3.xlsx]Sheet1'!
So, as far as I am concerned, so far so good!
However, I am embarrassingly stumbling at the last hurdle. when I use the Final defined Name in cell references, it does not resolve in the way I expect. So, if I am in Book1 and I use the code above to refer to cell A3 (which for example contains the word "Success!") in Book3, I would hope that typing the following into a cell in Book1 would resolve to "Success!":
=Full&A3
Instead, the A3 resolves to A3 in Book1.
You'd need to use INDIRECT for that to work, but the other workbook would have to be open.
http://www.cpearson.com/excel/indirect.htm
As the workbook has to be open for it to work there's no need to add the file location to the string. It may be useful in some VBA code used in the Workbook open event to automatically open the other workbook though, or look at Excel workspaces to open the other files.
http://www.ozgrid.com/Excel/save-as-workspace.htm
i.e. =INDIRECT("'[Book3.xlsx]Sheet1'!$A$1") or =INDIRECT(Final) if Final is a named value.

Wildcard reference to another workbook with INDIRECT Function

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.

Resources