Auto save and rename file

Jmtramos

New Member
Joined
Sep 29, 2020
Messages
3
Office Version
  1. 2016
Platform
  1. Windows
Hello everyone,

I have a timesheet that I need to send every month to my company. I also have to rename it with the current date (Year and Month) and add a code has shown below.
Off course, it's not right because it gives me an error when opening saying:

Error.jpg


My question is, how can I save to xlsx file format and remove all macros from file.

VBA Code:
Sub SaveAsNew()
Dim dt As String, wbName As String

wbName = "_KT_TS_KT055I-Jorge Ramos"
dt = Format(CStr(Now), "YYYYMM")
Application.DisplayAlerts = False
ActiveWorkbook.SaveCopyAs FileName:="C:\TimeSheet\" & dt & wbName & ".xlsx"
End Sub

VBA Code:
Private Sub Workbook_AfterSave(ByVal Success As Boolean)
If Success = True Then Call SaveAsNew
End Sub

Many thanks to all!
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Hi Jmtramos, welcome to this board.
The code below should do what you're after. Put this code in the workbook of which you want to have two different versions.

This goes in the ThisWorkbook module:
VBA Code:
Private Sub Workbook_AfterSave(ByVal Success As Boolean)
    If Success Then SaveWithoutMacros
End Sub

This goes in a standard module:
VBA Code:
Public Sub SaveWithoutMacros()
    Dim dt As String, wbName As String

    wbName = "_KT_TS_KT055I-Jorge Ramos"
    dt = Format(CStr(Now), "YYYYMM")
    Application.DisplayAlerts = False

' end up with original file on desktop
    Dim sOrgWbFullName  As String
    sOrgWbFullName = ThisWorkbook.FullName

' disable the Workbook_AfterSave event macro to avoid endless recursion
    Application.EnableEvents = False

' save copy of workbook without macro's
' this running macro however stays within memory and will proceed (!!)
    ThisWorkbook.SaveAs Filename:="C:\TimeSheet\" & dt & wbName & ".xlsx", _
                        FileFormat:=xlOpenXMLWorkbook, _
                        ConflictResolution:=xlLocalSessionChanges
    Application.EnableEvents = True
    Application.DisplayAlerts = True
    
' return to where we left of ===
    Application.Workbooks.Open Filename:=sOrgWbFullName
               
' === release current .xlsx (!!) workbook and its macros from memory ===
    ThisWorkbook.Close
           
    MsgBox "This message will never be displayed..."
End Sub
 
Upvote 0
Hi Jmtramos, welcome to this board.
The code below should do what you're after. Put this code in the workbook of which you want to have two different versions.

This goes in the ThisWorkbook module:
VBA Code:
Private Sub Workbook_AfterSave(ByVal Success As Boolean)
    If Success Then SaveWithoutMacros
End Sub

This goes in a standard module:
VBA Code:
Public Sub SaveWithoutMacros()
    Dim dt As String, wbName As String

    wbName = "_KT_TS_KT055I-Jorge Ramos"
    dt = Format(CStr(Now), "YYYYMM")
    Application.DisplayAlerts = False

' end up with original file on desktop
    Dim sOrgWbFullName  As String
    sOrgWbFullName = ThisWorkbook.FullName

' disable the Workbook_AfterSave event macro to avoid endless recursion
    Application.EnableEvents = False

' save copy of workbook without macro's
' this running macro however stays within memory and will proceed (!!)
    ThisWorkbook.SaveAs Filename:="C:\TimeSheet\" & dt & wbName & ".xlsx", _
                        FileFormat:=xlOpenXMLWorkbook, _
                        ConflictResolution:=xlLocalSessionChanges
    Application.EnableEvents = True
    Application.DisplayAlerts = True
   
' return to where we left of ===
    Application.Workbooks.Open Filename:=sOrgWbFullName
              
' === release current .xlsx (!!) workbook and its macros from memory ===
    ThisWorkbook.Close
          
    MsgBox "This message will never be displayed..."
End Sub

Hello GWteb,

Thanks a lot for the help.
I will try to change my code and will tell you if it works.
 
Upvote 0
Once again, I want to thank you. It worked on the first try.
 
Upvote 0
You're welcome and thanks for letting me know.
 
Upvote 0

Forum statistics

Threads
1,214,875
Messages
6,122,046
Members
449,063
Latest member
ak94

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