VBA Code that will save the current workbook multiple times

Craig_Moore

Board Regular
Joined
Dec 12, 2018
Messages
64
Office Version
  1. 2019
Platform
  1. Windows
Hi I hope someone can help me

I currently have to copy and paste a master copy of a workbook every day and rename it to the current date I.E 01.01.20 but I am after a button I can press that will automatically save the workbook 356 / 366 times naming all the workbooks a different date is this possible

thanks
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
try this:

VBA Code:
Sub MultiSave()
    Dim sFilename As Variant
    Dim sName As String
    Dim sExtension As String
    
    Dim dDate As Date
    Dim nLeapYear As Integer
    Dim n As Integer 'date counter
    
    sFilename = Application.GetSaveAsFilename(fileFilter:="Excel Files (*.xlsx; *.xlsm), *.xlsx; *.xlsm")
    
    If sFilename <> "" Then
        sName = Left(sFilename, InStr(sFilename, ".") - 1)
        sExtension = Right(sFilename, Len(sFilename) - Len(sName))
        
        dDate = DateValue("1 Jan 2020")
        If MsgBox("Is this a leapyear?", vbYesNo) = vbYes Then
            nLeapYear = 1
        Else
            nLeapYear = 0
        End If
        
        For n = 1 To 364 + nLeapYear
            sFilename = sName & " - " & Format(dDate, "dd-mmm-YYyy") & sExtension
            ActiveWorkbook.SaveAs Filename:=sFilename
            dDate = dDate + n
        Next n
    End If
End Sub
 
Upvote 0
Hi pjmorris
thank-you for the quick response i have tried the above and it partiality works it saves the files but dose not save a file fore every day it seems to jump i.e 01-01-20, 03-01-20, 16-01-20
also when running the code it doesn't end until i force it to stop

any further help would be greatly received

thanks

Craig
 

Attachments

  • saves.png
    saves.png
    97.6 KB · Views: 13
Upvote 0
HI Craig,

Many thanks for the screen shot - it has helped me understand the problem. Thankfully the cure is simple, just change:

VBA Code:
dDate = dDate + n

to

VBA Code:
dDate = dDate + 1

It might seem that the code doesn't end, but I suspect it is just a much slower process than expected. I've added a line to the code below which updates the status bar at the bottom of the excel window which shows what date is being exported. That should give you a clear indication of how its going. It includes the correction above.

VBA Code:
Sub MultiSave()
    Dim sFilename As Variant
    Dim sName As String
    Dim sExtension As String
   
    Dim dDate As Date
    Dim nLeapYear As Integer
    Dim n As Integer 'date counter
   
    sFilename = Application.GetSaveAsFilename(fileFilter:="Excel Files (*.xlsx; *.xlsm), *.xlsx; *.xlsm")
   
    If sFilename <> "" Then
        sName = Left(sFilename, InStr(sFilename, ".") - 1)
        sExtension = Right(sFilename, Len(sFilename) - Len(sName))
       
        dDate = DateValue("1 Jan 2020")
        If MsgBox("Is this a leapyear?", vbYesNo) = vbYes Then
            nLeapYear = 1
        Else
            nLeapYear = 0
        End If
       
        For n = 1 To 364 + nLeapYear
            sFilename = sName & " - " & Format(dDate, "dd-mmm-YYyy") & sExtension
            Application.StatusBar = "Exporting File Dated: " & dDate
           
            ActiveWorkbook.SaveAs Filename:=sFilename
            dDate = dDate + 1
           
        Next n
    End If
End Sub

HTH
 
Upvote 0
Solution
hi pjmorris

thankyou so much for this, this code has worked perfectly


Craig
 
Upvote 0

Forum statistics

Threads
1,223,884
Messages
6,175,171
Members
452,615
Latest member
bogeys2birdies

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