Refer to external files from variable

Trevor BBL

New Member
Joined
Aug 23, 2021
Messages
5
Hi, I am somewhat newer to more advanced excel operations.

I am managing my company's safety statistics in a database for each year and I am trying to develop a script that will show 3 and 5 year previous totals for different statistics. I have setup the information by manually referencing the previous years and referencing the variables from those workbooks, but I wanted to set this up in the template to automatically reference the right previous years based on the data from another cell

For example
=SUM('Z:\Health and Safety\Statistics\[Safety Yearbook 2020.xlsx]Annual'!B4)

How do I have the year in that file name be drawn from a per-defined cell?
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
You should use INDIRECT, but it won't work on closed workbooks.
The other option, short of vba, is powerquery if the other workbook is not protected.
 
Upvote 0
You should use INDIRECT, but it won't work on closed workbooks.
The other option, short of vba, is powerquery if the other workbook is not protected.

Power query will not work well as it will create a new sheet for each one and I still can't script it. Indirect won't work as it isn't practical to keep 5 years of statistics open at a time.

Visual Basic is pretty easy, how do I script this in VBA?
 
Upvote 0
Power query will not work well as it will create a new sheet for each one and I still can't script it
Both your statements above are wrong.
Vba handling closed workbooks is quite a tedious task. Far from easy.
 
Upvote 0
Maybe, I misunderstood then, how do I power query this so that it is a template that will load the variables from the annual summary from each of the 5 previous years sheets based on the value in B1
 
Upvote 0
I just had a slightly different idea. Probably by far the simplest of all ways to do it.
What are the formulas you are using now? What do cells do you want to reference for the workbook names? Is anyting else changing besides the workbook names?
Shortly, my idea is to use vba to write the formulas for you once you provide the necessary data: workbook locations, sheetnames, cell address(es).
 
Upvote 0
I am currently only tracking 3 variables so far (if I have a easy to understand solution maybe I will track more in the future) from each year for each sheet to get the Total Recordable incident Frequency and and Lost Time incident Frequency
Total man hours B4 B6 and B11 of !Annual sheet for each year. The rest is just a function on those 3 variables.
Here is the section (41-46) in the template based on 2021's previous years using manually configured external links from 2021. I have included the code in the 2020 tab to clarify what the table looks like The 3 and 5 year total sections just draw on the data from within this set.
Years prior statistics2020 {=SUM(B1-1)}
2019​
2018​
3 Year total
2017​
2016​
5 Year total
Hours Worked95819 {=SUM('Z:\Health and Safety\Statistics\[Safety Yearbook 2020.xlsx]Annual'!B4)}
72728​
57088​
225635​
49767​
39270​
314672​
Total Recordables3 {=SUM('Z:\Health and Safety\Statistics\[Safety Yearbook 2020.xlsx]Annual'!B6)}
0​
3​
6​
1​
0​
7​
Total Recordable Frequency6.26 {=SUM((B43*200000)/B42)}0.0010.515.324.020.004.45
Total lost time1 {=SUM('Z:\Health and Safety\Statistics\[Safety Yearbook 2020.xlsx]Annual'!B11)}
0​
1​
2​
1​
0​
3​
Lost Time Frequency2.09 {=SUM((B45*200000)/B42)}0.003.50
1.77​
4.02​
0.00​
1.91​
 
Upvote 0
@Trevor BBL Are you just trying to do 3 separate reads of one cell value from the same closed workbook? B4,B6, & then B11 from the closed workbook?
 
Upvote 0
- I hote the curly brackets do not indicate array formulas
- You dont really need SUM function to get a cell's value

This is what I came up with: you setup a table, name it tbRefs and a button to extract the data from various workbooks.
1629877385842.png

Like I said in my previous post the idea is to construct the formula and leave Excel take the values out of any closed workbook, if it is acessible.
So in the last column of the table the necessary formulas will be written and the values will turn up if everything is correct. Once you have the values you can use them anywhere in your workbook.
To automate the process create a button next to the table (on the same sheet) and make it run the following sub (put this in a public module):
VBA Code:
Option Explicit
Option Compare Text

Sub MakeFormulas()
'#Creates formulas to refer to external file based on provided references to the data
'# no error checking is implemented - the formula will return an error if references are incorrect
    Dim li As ListObject, cc As Range
    Dim i As Long, j As Long, strf As String
    On Error Resume Next
    Set li = ThisWorkbook.ActiveSheet.ListObjects("tbRefs")
    If li Is Nothing Then GoTo ep
    If li.ListRows.Count < 1 Then GoTo ep
    For i = 1 To li.ListRows.Count
        With li.ListRows(i).Range
            strf = .Cells(1, 1).Value & Application.PathSeparator 'get file path
            strf = "'" & strf & "[" & .Cells(1, 2) & "]" 'Add filename
            strf = strf & .Cells(1, 3) & "'!" 'Add sheet name
            strf = strf & .Cells(1, 4) 'Add cell address
            Select Case LCase(.Cells(1, 5).Value) 'This will endable you to provide different types of formulas for different references
                Case "" 'Just a reference to the cell
                    strf = "=" & strf 'make formula - just a reference to a cell value
                Case "sum"
                    strf = "=SUM(" & strf & ")" 'Anodther formula - this enables you to provide range instead of cell. However this option will return 0 if the cell contains text
                Case Else
                    'More options can be included - the sky is the limit :)
                    strf = ""
            End Select
            .Cells(1, 6).Formula = strf
        End With
    Next i
   
ep:
    On Error Resume Next
    Set li = Nothing
    Set cc = Nothing
   
End Sub
MrExcel Playbook 01 2021-08.xlsm
ABCDEFG
1PathFileSheetCellFormulaValue
2c:\Users\bobsa\DesktopWorkbook1.xlsxSheet1B2B24546
3c:\Users\bobsa\DesktopWorkbook1.xlsxSheet1i4Paul Oakley
4c:\Users\bobsa\DesktopWorkbook1.xlsxSheet1T30.333333333
5c:\Users\bobsa\DesktopWorkbook4.xlsxSheet 2222P1:P3SUM750
6c:\Users\bobsa\DesktopWorkbook4.xlsxSheet 2222D3Warehouse
7
extFile
Cell Formulas
RangeFormula
F2F2='c:\Users\bobsa\Desktop\[Workbook1.xlsx]Sheet1'!B2
F3F3='c:\Users\bobsa\Desktop\[Workbook1.xlsx]Sheet1'!I4
F4F4='c:\Users\bobsa\Desktop\[Workbook1.xlsx]Sheet1'!T3
F5F5=SUM('[Workbook4.xlsx]Sheet 2222'!P1:P3)
F6F6='[Workbook4.xlsx]Sheet 2222'!D3


if the cell in column E (Formula) is:
- empty - you will get a simple cell reference as a formula
- SUM - the formula will use SUM(), in this case you can provide a range reference like A5:B15 in the Cell column (D)
- you can add more options to this one - then you have to write an additional CASE statement in the sub

Whenever you change a vlaue in columns A to E push the button to update column F.
You will get a popup message from Excel if you provide a non-existent sheet name for a file.
Give it a try.
 
Upvote 0
@Trevor BBL Are you just trying to do 3 separate reads of one cell value from the same closed workbook? B4,B6, & then B11 from the closed workbook?
No, there is a separate one for each year that has to be referenced in the respective column for that year. I need the file name to be altered based on the value in the year column which is derived by whatever the current year entered in B1 of this sheet is. so for 2020 I need to load from [safety yearbook 2020.xlsx] and for 2019 [safety yearbook 2019.xlsx], the same cells are referenced across the tables because it's from the same template. But I have to automate it so that I can hand it off without making someone go through an extensive process of editing external links every year.

I have a bunch of meetings today, so I don't know if I will get a chance, but I am hoping Bobsan's VBA script will do the trick.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top