Save macro but adds a .1 to the saved file

Theoutlaw700

New Member
Joined
Jun 8, 2022
Messages
8
Office Version
  1. 365
Platform
  1. Windows
I already have a macro that has a save function I will list it below, but what I want to happen is I want it to count up every time I save. For example I hit my save macro and it makes a file in my intended loaction but the file name is example.1 then the next time I hit the macro it will save but the file will be saved as example.2 and so forth. Is this possible?

Thank you for your help in advance!

VBA Code:
Sub test()
Dim path As String
Dim filename1 As String

path = "C:\CSV\"

Application.DisplayAlerts = False

ThisWorkbook.Sheets("Data").Copy


filename1 = "Schedulecsv"
Application.DisplayAlerts = True
ActiveWorkbook.SaveAs filename1, FileFormat:=62, CreateBackup:=False
ActiveWorkbook.Close

VBA.MsgBox "Saved'"


End Sub
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
So to be clear are you wanting it to say Schedulecsv.1 then next time Schedulecsv.2?
 
Upvote 0
Okay i think i have your Solution

Add this code to "Thisworkbook" as "AfterSave"

VBA Code:
Private Sub Workbook_AfterSave(ByVal Success As Boolean)

Range("A1") = Range("A1") + 1

End Sub

Change A1 to a Cell of your choosing some where in the sheet that wont harm what your doing. this will provide a counter to base your code from

Then try this code

Code:
Sub test()
Dim path As String
Dim filename1 As String
Dim Count As String

path = "C:\CSV\"

Application.DisplayAlerts = False

ThisWorkbook.Sheets("Data").Copy

Count = Range("A1")
filename1 = "Schedulecsv"
Application.DisplayAlerts = True
ActiveWorkbook.SaveAs filename:=Filename1 & "-" & Count & ".xls", FileFormat:=62, CreateBackup:=False
ActiveWorkbook.Close

VBA.MsgBox "Saved'"

Again Change "A1" to where ever you had decided the counter destination

you never wanna use "." unless you are designating a file extension so i switched it to Hyphen so your file should open
 
Upvote 0

Forum statistics

Threads
1,214,918
Messages
6,122,246
Members
449,075
Latest member
staticfluids

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