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?
 
but I am hoping Bobsan's VBA script will do the trick
It works, just let us know if it's useful in your context.
You can add or remove rows with references as you like. While they are in the table they will be processed.
I just got an idea: you can add a seventh column to the table (or more if you need) to explain the meaning of each value - this should be useful for other users.
You can also produce the filenames with a formula, but upon changing them you will have to run the script again to rebuild the formulas in column F.
 
Upvote 0

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
- 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.
View attachment 45573
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.
Hi Bobsan,

I was searching for a solution to this very problem and this has worked perfectly! I stripped out some of the cell dependant forumla as I only wanted one cell with the filename controlling it and have ended up with:

Private Sub CommandButton1_Click()
'#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("tbRefs4")
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 = "T:\CUSTOMER LIBRARY\2 Quote Log" & Application.PathSeparator 'get file path
strf = "'" & strf & "[" & .Cells(1, 1) & "]" 'Add filename
strf = strf & "Batch'!" 'Add sheet name
strf = strf & "$Y$5" 'Add cell address
Select Case LCase(.Cells(1, 100).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, 2).Formula = strf
End With
Next i

ep:
On Error Resume Next
Set li = Nothing
Set cc = Nothing
End Sub

Theres one additional bit that I would love if you could help me with. I'd like to have the option to create more forumlas at the same time. For example in the 3rd column, to have the same pathway as before but pull the data from cell L21 ( strf = strf & "$L$21" 'Add cell address). Hopefully this is something that can be done? If I could expand it further to have the 4th column as cell D5 etc etc, would be fantastic!
 
Upvote 0
Theres one additional bit that I would love if you could help me with. I'd like to have the option to create more forumlas at the same time. For example in the 3rd column, to have the same pathway as before but pull the data from cell L21 ( strf = strf & "$L$21" 'Add cell address). Hopefully this is something that can be done? If I could expand it further to have the 4th column as cell D5 etc etc, would be fantastic!
I do not completely understand your scenario, but for the moment all I can say is that you can make the table with references as long as you need if.
You should be able to pull values from as many cells, ranges, sheets and workbooks as you like. Probably with some performance costs :)
I believe it is as simple as possible and I would like to keep it that way.
 
Upvote 0
Hi Bobsan,

I am essentially trying to change the script input to have multiple formula inputs in a row, rather than a single one. However I can just do a single input per row and then sort into a second table based on certain factors so no worries. Thanks anyways!
 
Upvote 0
I think it's best to keep it simple, and do the complicated formulas after you pull the data - it will be faster and you'll have less potential for errors.
 
Upvote 0

Forum statistics

Threads
1,223,912
Messages
6,175,340
Members
452,638
Latest member
Oluwabukunmi

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