Variable Referencing for Workbook Names and File Paths VBA

turner38

New Member
Joined
Jul 6, 2011
Messages
32
Hello All,

I am trying to make my reference for "August.xlsm" so that if it was renamed (i.e. "August 8-15-11") the code would still know to reference that workbook. Is this possible?

Also I have the same issue in the next part of my code. I want to save the "September.xls" workbook to "My Documents" for which ever user has the file. So if the user was not Jim Lewis it would still save the workbook to the current user's "My Documents". Would this be possible?

Code:
    Workbooks.Add
    Sheets("Sheet1").Name = "Data"
    Workbooks("August.xlsm").Sheets("Data").Range("$A$1:$O$50000").Copy
    ActiveWorkbook.Sheets("Data").Range("$A$1:$O$50000").PasteSpecial Paste:=xlPasteAllUsingSourceTheme
 
    ActiveWorkbook.SaveAs Filename:= _
        "C:\Documents and Settings\Jim Lewis\My Documents\September.xls", FileFormat:= _
        xlExcel8, Password:="", WriteResPassword:="", ReadOnlyRecommended:=False _
        , CreateBackup:=False

Thanks,
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
You should be able to swap out user name with:
Application.UserName

"C:\Documents and Settings\" & Application.UserName & "\My Documents\September.xls", FileFormat:= _


And referencing the file you are actively in can be done with:
ActiveWorkbook

ActiveWorkbook.Sheets("Data").Range("$A$1:$O$50000").Copy

You could also create a dynamic name for the save as file name...

For example if you want to name it by the name of what ever month is the month after the month you are in:
FileMoName = Format(DateSerial(Year(Now()), Month(Now()) + 1, 1), "Mmmm")

"C:\Documents and Settings\" & Application.UserName & "\My Documents\" & FileMoName& ".xls", FileFormat:= _
 
Upvote 0
Thanks. The "Application.Username" gives me the author of the excel file, but I am needing the Windows Username. This seems to work:

Code:
UNameWindows = Environ("USERNAME")
    ActiveWorkbook.SaveAs Filename:= _
        "C:\Documents and Settings\" & UNameWindows & "\Desktop\September.xls", FileFormat:= _
        xlExcel8, Password:="", WriteResPassword:="", ReadOnlyRecommended:=False _
        , CreateBackup:=False
 
Upvote 0

Forum statistics

Threads
1,224,600
Messages
6,179,836
Members
452,947
Latest member
Gerry_F

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