Get file path from excels cell - excel

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.

Related

How to reference a cell as table range when using vlookup

I have vlookups to pull specific data from a workbook and paste into a new workbook in the desired layout. The layout of the first workbook never changes however the name will change when i want to run this on a different file.
My current formula is =VLOOKUP(A3,[Workbook1.xlsx]Sheet1!$B$3:$XFD$7,2,FALSE)
I would really like it to reference A1 instead of Workbook1 so I could then just update the file name in A1 every time I want to analyse a different file. I should mention the Sheet name won't ever change.
I know you have to use INDIRECT but im unsure how it works. I did try =VLOOKUP(A3,INDIRECT(A1),$B$3:$XFD$7,2,FALSE) but then i'd too many arguments and when i removed the $B$3:$XFD$7 i lost the range i was searching in.
Thanks!
With INDIRECT you must create the whole string that denotes the range reference:
=VLOOKUP(A3,INDIRECT("'[" & A1 & "]Sheet1'!$B$3:$XFD$7"),2,FALSE)
One more note, that INDIRECT requires that the workbook be open to function, or will return an error.

Load single cell in query in excel

I have n excel files with the same layout, and i want to create a recap file where some info is reported for each file. The info is located in the same specific cells, for example d2 and e2 in every file.
What is the best way to do it? The file names vary without logic and that people that access that file have no skills in coding and macro, so i try to develop something very simple.
I also have no skill in database, but i have managed to create a query with every file in the folder, i have then created a column with the data but i am not able to say that i only want a specific cell, is there a way to do it?
here is a picture of the situation
A formula using INDIRECT can be used, but it will generate a #REF error when the workbook is closed. you could use your macro to open the relevant workbook, and then close it after saving the values
the indirect formula would look something like this:
=INDIRECT("'[Test File.xlxs]Test Data'!$A$7")
or, as a formula using your data:
=INDIRECT("[" & B1 & "]YourSheet!$E$2")

Reference Excel files getting filename from a cell

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".

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.

Paste excel link formula using vba

I am working in an organization where we have 100s of excel files on our shared drive. These excel files contain asset numbers to our company assets in different cells within the files. What I would like to do is create a script in vba which would run only once and search through all these excel files using the find method and paste their cell addresses adjacent to their file path (I already have the paths of all the files in an excel sheet). But this address should in the form of a link formula, just like "=A2" so that we can later on modify this formula if there is a change in the position of the asset number without using vba. So suppose,
If cell E3 in the file1 contains asset number "XXX0000", the vba script should paste something like " ='\shared drive\folder path\file1.xlsx]WS'!$E$3 " in the cell next to the path,"\shared drive\folder\". So later on we can manually modify this formula if there is a change in its cell location
Thanks

Resources