Update filenames in multiple modules

cadlum

New Member
Joined
Apr 15, 2013
Messages
16
Hi all,
This is a weird and specific problem so I was having trouble finding anything on google.

I have 6 modules that do reporting on a computer inventory database (xlsx format). The reports work great but I plan on handing this stuff out to others. I would like it to work without them having to go into the code at all. I am trying to create another module that will go to each of the other 6 and update the file paths they are using.

Currently, each module start off like this (after the dim's)
Code:
FilePath = Left(ActiveWorkbook.Path, InStrRev(ActiveWorkbook.Path, "\"))
FileName1 = "Exmaple.xlsx"


Workbooks.Open FileName:=FilePath & "\" & FileName1

The code also ends with closing the open workbook.

Code:
Workbooks(FileName1).Close SaveChanges:=False

I want to update FileName1 in each module. This section of code is not on the same line numbers in the modules. So I was hoping for a more flexible approach. I would like to be able to have a module that will ask the user what file they want to use and then it will update all the modules dynamically (even if their name changes).

This is the code I have so far, but its more static than dynamic.

Code:
Sub UpdateFileLoc()
'for this macro to work, this setting needs to be set manually
'Office button > Excel Options > Trust Center > Trust Center Settings > Macro Settings, check Trust access to the VBA project


Dim sFileName As Variant, iNum As Integer, strFile As String, tests As String, FileName As String


sFileName = Application.GetOpenFilename(FileFilter:="Inventory Files (*.xls; *.xlsx),*.xls;*.xlsx", _
            Title:="Open Inventory File", MultiSelect:=True)


If IsArray(sFileName) Then
    For iNum = LBound(sFileName) To UBound(sFileName)
        strFile = sFileName(iNum)
        FileName = Mid(strFile, InStrRev(strFile, "\") + 1)
        Application.VBE.ActiveVBProject.VBComponents("test").CodeModule.ReplaceLine 9, "FileName1 = """ & FileName & """"
        'set up error handling if they havent made the settings change
    Next iNum
End If


End Sub

Thanks!
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Hi; either use a public variable. How to use variables in Excel sub-procedures in Visual Basic for Applications

For example:
Code:
Public sFilename As String

Sub setsfilename()
sFilename = "Hello.xlsx"
End Sub

Sub anothermacro()
MsgBox sFilename
End Sub


Sub anotheranothermacro()
MsgBox sFilename
End Sub

Or store the file name in a spare cell in a spare sheet.

For example:
Code:
Sub setsfilename()
Sheets("Sheet1").Value = "Hello.xlsx"
End Sub

Sub anothermacro()
Dim sFilename As String
sFilename = Sheets("Sheet1").Value = "Hello.xlsx"
MsgBox sFilename
End Sub

Sub anotheranothermacro()
Dim sFilename As String
sFilename = Sheets("Sheet1").Value = "Hello.xlsx"
MsgBox sFilename
End Sub
 
Upvote 0
Oops! the second example should be as below>

Code:
Sub setsfilename()
Sheets("Sheet1").Value = "Hello.xlsx"
End Sub

Sub anothermacro()
Dim sFilename As String
sFilename = Sheets("Sheet1").Value
MsgBox sFilename
End Sub

Sub anotheranothermacro()
Dim sFilename As String
sFilename = Sheets("Sheet1").Value
MsgBox sFilename
End Sub
 
Upvote 0

Forum statistics

Threads
1,221,531
Messages
6,160,374
Members
451,642
Latest member
mirofa

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