[VBA] update ActiveWorkbook's name when content changes via [save] button

smallxyz

Active Member
Joined
Jul 27, 2015
Messages
392
Office Version
  1. 2021
Platform
  1. Windows
The following code works well to rename activeworkbook and remove the old workbook when it detects content changes. [like a time-stamp saving ]

However, after execution, the Excel application itself just crashes down even "On Error Resume Next" is included.
Nonetheless, I can still open the newly renamed workbook successfully ( all changed content also remains successfully)

But it is strange and inconvenient in the way that, whenever I press [save], the Excel closes itself. And I get to re-open the workbook to continue working on it instead of being able to stick continuously to the activebook for further editing.

Anything wrong?

I am currently using Excel 2012.

Thanks!



Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    On Error Resume Next
    '---------------------
    With ActiveWorkbook
        '---------------------
        '   [ Unsave if no changes ]
        
        If (.Saved = True) Then
            Cancel = True
            Exit Sub
        End If
        '---------------------
        '   [ Save if changes ]
        
        old_path = .FullName
        new_path = .Path & "\" & "Test" & Format(Now(), "yy-mm-dd")
        Cancel = False
        
        If Dir(new_path, vbNormal) <> "" Then
            DoEvents
        Else
            .SaveAs Filename:=new_path, _
                FileFormat:=xlOpenXMLWorkbookMacroEnabled
            Kill (old_path)
        End If
        '---------------------
    End With
End Sub




Below is the crash report:

問題簽章:
問題事件名稱: APPCRASH
應用程式名稱: EXCEL.EXE
應用程式版本: 15.0.4535.1507
應用程式時間戳記: 52282d17
錯誤模組名稱: EXCEL.EXE
錯誤模組版本: 15.0.4535.1507
錯誤模組時間戳記: 52282d17
例外狀況代碼: c0000005
例外狀況位移: 000000000013f642
作業系統版本: 6.1.7601.2.1.0.256.48
地區設定識別碼: 3076


與問題相關的其他資訊:
LCID: 1028
skulcid: 3076


閱讀我們的線上隱私權聲明:
Windows 7 Privacy Statement

Thanks a lot!
 
Last edited:

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    '---------------------
    With ThisWorkbook
        '---------------------
        '   [ Unsave if no changes ]
        
        If (.Saved = True) Then
            Cancel = True
            Exit Sub
        End If
        '---------------------
        '   [ Save if changes ]
        
        old_path = .FullName
        new_path = .Path & "\" & "Test" & Format(Now(), "yy-mm-dd hhmmss") & ".xlsm"
        
        If Dir(new_path, vbNormal) <> "" Then
            DoEvents
        Else
            Application.EnableEvents = False 'stops saveas from firing this BeforeSave event again
            Cancel = True 'cancels original save request that got you here
            .SaveAs Filename:=new_path, FileFormat:=xlOpenXMLWorkbookMacroEnabled
            Application.EnableEvents = True
            Kill (old_path)
        End If
        '---------------------
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,643
Messages
6,120,702
Members
448,980
Latest member
CarlosWin

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