Filename in VBA variable

Lothrian

New Member
Joined
Jun 13, 2023
Messages
5
Office Version
  1. 365
Platform
  1. Windows
I am trying to build an analytics tool, I fully expect the end users of the tool to change the file name of the tool for their own purposes.

How would I write the file name as a variable when referencing the file the macro is imbedded into.

EG. the tool is copying a data sets from a location to the existing workbook, *names changed for anonymity*

Sub Example()

'Copy data from "Sheet 1" Download Sheet
strFile = Application.GetOpenFilename()
Workbooks.Open (strFile)
Filename = ActiveWorkbook.Name
Sheets("Sheet 1").Copy After:=Workbooks("Book 1.xlsm").Sheets(2)
Workbooks(Filename).Close savechanges:=False
Workbooks("Book 1.xlsm").Activate
Worksheets("Sheet 1").Activate
ActiveSheet.Outline.ShowLevels RowLevels:=2
ActiveSheet.Name = "Sheet 2"
Sheets("Sheet 2").Select

End Sub

... if the user was to change the save name of the workbook from "Book 1.xlsm" the macro will fail to run,

Can I set this as a variable so the code will look at the existing save name and use that as the reference?

EG
Dim Filename As ....
....

'Copy data from "Sheet 1" Download Sheet
strFile = Application.GetOpenFilename()
Workbooks.Open (strFile)
....
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Welcome to the Board!

If you put this code at the very beginning of your procedure, it will dynamically capture the workbook with macros:
VBA Code:
Dim wb as Workbook
Set wb = ThisWorkbook
and then you can refer/go back to it later in your code like this:
VBA Code:
wb.Activate

So you do not need to know the name of the workbook at all, but if you did want to see what it was, you could use:
Excel Formula:
MsgBox wb.Name
 
Upvote 0
Welcome to the Board!

If you put this code at the very beginning of your procedure, it will dynamically capture the workbook with macros:
VBA Code:
Dim wb as Workbook
Set wb = ThisWorkbook
and then you can refer/go back to it later in your code like this:
VBA Code:
wb.Activate

So you do not need to know the name of the workbook at all, but if you did want to see what it was, you could use:
Excel Formula:
MsgBox wb.Name
Lovely thanks, I'll give that a try.
 
Upvote 0

Forum statistics

Threads
1,215,214
Messages
6,123,664
Members
449,114
Latest member
aides

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