I am creating a excel file that will pull a daily report from a folder on a different drive everyday. I was wondering if there is a way to increment a file name in the formula by 1 easily?
The formula I am currently using is:
=VLOOKUP("Oil Production(Sm³)",'G:\DPNA\Non_Op_Assets\Hib\Pe\Production\Reports\Daily Report\2019\01 Jan19\[190113 Daily Report.xls]Daily Report'!$A$1:$P$500,3,FALSE)*1
I was wondering If it is possible to be able to get the 190113 to increment by one for the next 365 cells easily.
Not easily. It looks to me that not only do you need to increment the 190113, but it will need to flip over to 190201 for the 1st February, and also the file path has the year and the month spelled out.
This can be constructed with formulas, though. You can create the formulas that generate the path and file name bit automatically.
="Daily Report\"&YEAR(A2)&"\"&TEXT(A2,"dd MMMyy")&"\["&TEXT(A2,"yymmdd")&" Daily Report.xls]Daily Report'!$A$1:$P$500,3,FALSE)*1"
The next issue is how you use that. You could plug that into an Indirect function, but if Indirect refers to an external file, that file must be open for Indirect to work. Which defeats the purpose.
So, the other option is that you build the complete formula as a text string,
="=VLOOKUP(""Oil Production(Sm³)"",'G:\DPNA\Non_Op_Assets\Hib\Pe\Production\Daily Report\"&YEAR(A2)&"\"&TEXT(A2,"dd MMMyy")&"\["&TEXT(A2,"yymmdd")&" Daily Report.xls]Daily Report'!$A$1:$P$500,3,FALSE)*1"
then copy and paste the formula as values, then edit each pasted cell and confirm the formula it now contains.
Edit: VBA is the way to go to bulk edit, but you won't be able to record this, since you will need a loop.
Will the files be open? If so you can use indirect I just put a basic example together for you:
=INDIRECT("'C:\Reporting Archive\[Daily Sales and Orders Report - 2018121" & ROW(A1)+5 & ".xlsx]Summary - Volume'!$C$41")
I can drag this down and the 2018121 in the filename will start with a 6 then increment to a 7 then 8.
If you don't have the files open then you will need to use VBA. When reading from a closed workbook without opening it, I have always used the ExecuteExcel4Macro method like so:
strPath = "C:\Test\"
strFile = "Book1.xls"
strSheet = "Sheet1"
strRng = Range("A1")
strRef = "'" & strPath & "[" & strFile & "]" & strSheet & "'!" & strRng
Result = ExecuteExcel4Macro(strRef)
Related
I'm not sure if this is even possible in Excel but this is what I need to do:
I have a column with a list of hotels and then another column which needs to pull data from each individual hotel's excel file. For example, cell A2 will have the name "Paris" for the hotel and then cell B2 will have the link:
='G:\Hotels\Paris\Paris - Monthly\[Paris_summary_2018.xlsm]Feb'!$CD$89
I have lots of hotels I need to do this for in different sheets. So I need the link to dynamically refer to whatever hotel is in column A and the title of the sheet, for example could I do something like this?
=''G:\Hotels\A2\A2 - Monthly\[A2_summary_2018.xlsm]Feb'!$CD$89
where A2 has the string "Paris". Also is there a way to dynamically refer to "Feb" depending on what sheet I am in the month will be the title
I am also open to using VBA for this.
As long as you don't mind using VBA, you can easily generate the links with something like this:
Sub generate_hotel_links()
Dim r As Range, c As Range
Dim s As String
' This is the range which all the hotel-locations are in
Set r = ThisWorkbook.Worksheets("Sheet1").Range("A1:A10")
On Error Resume Next
For Each c In r
' Generate the formula based on the current cell we are in
s = "=" & Chr(39) & "G:\Hotels\" & CStr(c) & "\" & CStr(c) & " - Monthly\[" & CStr(c) & "_summary_2018.xlsm]Feb" & Chr(39) & "!$CD$89"
' ...and put it in the neighbouring cell
c.Offset(0, 1).Formula = s
Next c
On Error Goto 0
End Sub
On Error Resume Next will make the macro continue no matter what error pops up - the ideal case would be some more robust error handling, or a check for if the workbook / sheet actually exists before attempting to write the formula, but I'll leave it to you to attempt to write this if you feel the need improve the macro.
If you want to just use generic Excel formulas, I'd advice having a look at the question and answers I posted in the comments to your question.
I am not sure if you can use HYPERLINK.
Like this:
=HYPERLINK("G:\Hotels\"&A2&"\"&A2&" - Monthly\["&A2&"_summary_2018.xlsm]Feb!"&$CD$89)
I have a formula in I2:
= "=" & "'" & $H2 & $I2 & E2 & "'!" & "$C$2"
It combines the directory, filename, sheet name, and cell location.
Now I'd like to evaluate the result of that formula in J2. Is it possible to do so without VBA or with just a bit of VBA? I need to be able to fill down I2 to I1000 and I personally prefer a non-VBA approach.
For example, let's say the result of I2 is:
='R:\20180220\[Test.xlsb]'!$C$2
Is there a way to actually show the value of that cell in J2? Note that workbook Test.xlsb is closed and won't be opened (because I have thousands of workbooks and cannot open all of them).
I tried EVALUATE(Range.Formula) and ExecuteExcel4Macro() in VBA, and tried to wrap them with a UDF, but both failed.
Here's a kludge that may work for you.
Create the formulas as in your current post. Note that the result in your post lacks a reference to a tab/worksheet --- you likely need to update your formula to include a worksheet. The formula I used in my tests is = "=" & "'" & $PATH & $FILE & $TAB & "'!" & "$C$2" (the same as yours but with a field added for the $TAB).
Copy/paste special values the results. This replaces = "=" & "'" & $PATH & $FILE & $TAB & "'!" & "$C$2" with ='C:\PATH\WORKBOOK\TAB!$C$2 to give the correct formulas. Annoyingly they do not calculate automatically.
To force an update of the calcs, F9 doesn't seem to work, so select the results and do a mock find/replace, say replace $C$2 with $C$2. This doesn't change the formulas but gets the recalc going. You might also be able to just save, close and re-open the workbook.
Hope this helps.
I'm working on a cell formula that returns (via VLOOKUP?) a cell in a closed workbook (thus I believe INDIRECT will not work). The issue is that I want to use a value in the active sheet to determine the name of the sheet in the reference workbook and can't figure it out. Here's the best I've got.
=VLOOKUP($A3,'[Other Workbook.xlsm]ObsDFW'!$1:$800,COLUMNS($D4:D4)+3)
ObsDAL is the name of one of the sheets in the "Other Workbook". What I can't figure out is how to keep the "Obs" part of that name constant, but take the "DFW" from a cell value.
Using bad code, I want it to be:
=...[Master Statistics.xlsm]("Obs" & A1)'!$1:$800...
If context is helpful, the "Other Workbook" is full of hourly weather observations, separated into one sheet for each of a series of airports. I'm trying to pull this info into another file/workbook so I don't have to specify each airport specifically in the code many times over.
Thanks in advance!
You could try this VBA approach. This way you are adjusting Vlookup formula based on your dynamic_part (sheetname)
Sub VlookupClosedWorkbook()
Dim dynamic_part As Variant
dynamic_part = Range("B1").Value 'You should enter in cell B1 dynamic part of sheet name
For x = 3 To Range("A" & Rows.Count).End(xlUp).Row
Range("B" & x).Value = "=VLOOKUP(A" & x & ",'[Other Workbook.xlsm]Obs" & dynamic_part & "'!$1:$800,COLUMNS($D4:D4)+3,FALSE)"
Next x
End Sub
Assign this macro to shape and fire it after you change your dynamic part. When you trigger it for the first time, make sure that you have both Workbooks open.
I am trying to import a range of cells from a closed workbook.
I use the external reference link built into Excel:
='F:\UGR\JOB DATA SHEET\[JOB SHEETS 1-500.xlsx]JobNumber'!B4
='F:\UGR\JOB DATA SHEET\[JOB SHEETS 1-500.xlsx]JobNumber'!B5
...
Going down the column from B4:B23 and replicating that for columns B-Z.
This works if the sheet name doesn't change. But that file contains sheets for Jobs 1 - 500, each on their own sheet. I am trying to pull those columns of data for whatever JobNumber gets entered into cell "B7". So ideally it would look like this:
='F:\UGR\JOB DATA SHEET\[JOB SHEETS 1-500.xlsx]&B7&'!B4
='F:\UGR\JOB DATA SHEET\[JOB SHEETS 1-500.xlsx]&B7&'!B5
...
Etc.
I know this won't work without the Indirect function, but I need to have the other file open for that to work. This isn't practical given the number of users who are using this file for reference.
I found a macro in VBA that should do what I need, but I can't get it to work. Here is the base macro before I started messing around with it.
Function GetValue(Path, File, Sheet, Ref)
'Retrieves a value from a closed workbook
Dim Arg As String
'Make sure the file exists
If Right(Path, 1) <> "\" Then Path = Path & "\"
If Dir(Path & File) = "" Then
GetValue = "File not Found"
Exit Function
End If
'Create the argument
Arg = "'" & Path & "[" & File & "]" & Sheet & "'!" & Range(Ref.Range("A1").Address(, , xlR1C1))
'Execute XLM macro
GetValue = ExecuteExcel4Macro(Arg)
End Function
Any ideas on how to get it to work, or an alternative work around? I could also temporarily import the sheet to my other file and overwrite it when a new value is entered, thus importing another sheet from the other workbook, but that seems far more complex.
I am using Excel 2013.
UPDATE: I am closer to figuring it out but I cant get it to display anything but #Value errors. My formula looks like this in excel:
=GetValue(H11,H12,B7,B4)
Cell H11 = F:\UGR\JOB DATA SHEET\
Cell H12 = JOB SHEETS 1-500.xlsx
Cell B7 = The input cell where the user enters a JobNumber (aka sheet name).
Cell B4 = B4 (The cell I want to search on the external workbook)
Cell B4 is where I think the error lies. Will this macro be able to tell that it needs to search the external file at cell B4?
I figured it out. The macro cannot be launched from within the workbook itself, it must be done from VBA.
If I have a cell, "a2" which has part of a filename/filepath, "sheet2.xlsx" in it which I want to use as a reference another workbook, how can I accomplish this?
I have tried using the INDIRECT function but always get a reference error.
I am trying to find a way to summarize a list of spreadsheets with column a containing the spreadsheet name and another column referencing a cell inside that respective sheet.
If the referenced workbook is open, then here is an example of getting a value from it:
=INDIRECT( "'" & A1 & "[" & B1 & "]" & C1 & "'!" & D1)
where the components are like:
Note we have specified:
the path
the filename
the worksheet name
the cell address
If the file is not open, you can still retrieve data with a ExecuteExcel4Macro (assuming you have a version of Excel that supports this function) For an example of this type of code, see:
Get Data from External Files