Forum

Update VBA to Save ...
 
Notifications
Clear all

Update VBA to Save File to Location of Current File

5 Posts
2 Users
0 Reactions
870 Views
(@webbers)
Posts: 147
Estimable Member
Topic starter
 

I have a macro, and it works, however, it does not save the file to the correct folder.  I have this code in my template, and the macro saves a copy for audit purposes.  I would like the macro to save the audit file to the same location as the template is located.  I do not want any hard-coded paths, as this template will be provided to multiple users to have on their computers.  Thanks in advance for your help with this.  I am sure it is something simple, but I have no clue.

 

Sub SaveAudit()

' Save file as multiple cell references

ThisFile = Range("B17").Value & Format(Range("B16").Value, "mm-dd-yyyy") & ".xlsb"
ActiveWorkbook.SaveAs Filename:=ThisFile
End Sub

 
Posted : 02/04/2022 3:58 pm
(@jstewart)
Posts: 216
Estimable Member
 

The path name would be ThisWorkbook.Path, and that will return the path where the file is saved for each computer.

 
Posted : 03/04/2022 1:27 am
(@webbers)
Posts: 147
Estimable Member
Topic starter
 

@Jessica,

I thought I understood your response, but apparently not.  I revised my code, and when I ran it, I got an error.  Attached is a screenshot of the error, and the location where is showed when I attempted to debug.

Sub SaveAudit()

' Save file as multiple cell references

Dim ThisFile As String
Dim Path As String

ThisFile = Range("B17").Value & Format(Range("B16").Value, "mm-dd-yyyy") & ".xlsb"
Path = ThisWorkbook.Path & "" & ThisFile

ActiveWorkbook.SaveAs Filename:=ThisFile
End Sub

2022-04-05_16-18-10.jpg2022-04-05_16-18-35.jpg

 
Posted : 06/04/2022 4:20 pm
(@jstewart)
Posts: 216
Estimable Member
 

Your issue is this code right here:

ActiveWorkbook.SaveAs Filename:=ThisFile

I don't work with saving in VBA often, but I believe what you want instead is this:

ActiveWorkbook.SaveAs Filename:=Path

When saving with only the filename you want you haven't given it the path of where to save it.

 
Posted : 07/04/2022 10:49 am
(@webbers)
Posts: 147
Estimable Member
Topic starter
 

Thanks Jessica,

 

I knew it must have been something very simple. Sorry for my late response.  I was furloughed from my job and just returned this week.

 
Posted : 13/05/2022 10:15 am
Share: