Externally Referencing Data with Variable Sheet Names - excel

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.

Related

Refer ranges in a closed workbook with dynamic file path, INDEX(MATCH())

I'm using the Index(Match()) function to lookup a value from another closed workbook. The formula within cell B5 of the currently opened workbook is:
=INDEX('C:\Users\Downloads\[og.xlsx]Sheet1'!$B$1:$B$3,MATCH($A$5,'C:\Users\Downloads\[og.xlsx]Sheet1'!$A$1:$A$3,0))
where $A$5 stores the lookup value.
The path of source file C:\Users\Downloads\[og.xlsx]Sheet1 will change dynamically, and it is stored in the current workbook in cell C1 as text string without ' at either ends. I would like to have some help writing a user defined function (not sub!) in vba to replace the fomula as:
=INDEX(getValue($C$1,"$B$1:$B$3"),MATCH($A$5,getValue($C$1,"$A$1:$A$3"),0))
NOTE:
The source file should not open at any point, so INDIRECT() won't work.
Excel add-in is not preferred, so no INDIRECT.EXT().
The getValue(path,range) function should also work when range refers to a single cell value. (i.e. =getValue($C$1,"$B$1") should work as a cell formula alone)
I noticed the post Use file path from a predefined cell in a formula , which works fine for single cell value but not range when using index(match()). Also, I tried the following which returns #VALUE!:
Function GetValue(Rpath As String, Rrange As String)
Dim a, strRef As String
a = range(Rrange).Address(, , xlR1C1)
strRef = "'" & Rpath & "'!" & a
GetValue = ExecuteExcel4Macro(strRef)
End Function
Thanks in Advance!

Excel VLOOKUP Increment

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)

Use cell value/contents to determine an external reference file or sheet name?

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.

How to use a cell as a filename in a reference in excel?

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

ExecuteExcel4Macro to get range/charts from closed workbooks

I use these lines to get values from closed workbooks:
Arg = "'" & Path & "[" & File & "]" & Sheet & "'!" & "R4C4"
Arg = CStr(Arg)
GetValue = ExecuteExcel4Macro(Arg)
Is there another way than loop to get values from a range? The loop solution is working, but it would be clearer if I could get the range directly with ExecuteExcel4Macro. I've tried to input a range in Arg, but it returns an error.
I have the same question for charts, how can I get them? My solution for the moment consists of getting values and replotting the charts. It works, but I would be happier with a GetChart(Chartname) function.
I've seen that I can use ADODB connection to get value from closed workbooks. But it was a little too complex compared to ExecuteExcel4Macro. Would it be easier to use ADODB connection in the case of range/charts?
The following bit of code pulls info from a range in a closed workbook and copies it in the same ranges in the Active Workbook:
Sub GetRange()
With Range("A1:D50") 'set range to copy from / to.
.Formula = "='C:\E3_Test\[CC_Data.xlsx]AllData'!A1" 'refers to a workbook, sheet and first cell.
'It will put the relative references into the target sheet correctly.
.Value = .Value 'changes formula to value.
End With
End Sub

Resources