VBA different BeforeSave for Save and Save As

mrMadCat

New Member
Joined
Jun 8, 2016
Messages
39
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
  3. Web
Hello,
I need a macro to act differently on events Save and Save As.
There is Workbook.BeforeSave functionality in VBA, but is there something like BeforeSaveAs? Or is there a way around?
Thank you.
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Thank you for the link. After the reading I understand my question deeper.

My situation: I save into separate file info - file Name, User name and time. It's like log of user sessions.
On save it writes into the log file the end time of the session.
The problem is that if user saves as - he actually creates new file and closes the old one. This means that in the log file there should be two lines:
1) closed session of the old file (which works just fine now)
2) new active session line for the newly created file.

And I still don't understand how to do it. The next code isn't enough as I actually need 2 actions: one before old file is closed, and another after the new file is open.
Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
If Not SaveAsUI Then
'
' do stuff
'
End If
End Sub
 
Upvote 0
The code sample checks for save (i.e. SaveAsUI = False) and responds. If you want to respond differently to a saveas just add an Else to the If Block, e.g.

Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
If Not SaveAsUI Then
'
' do stuff for Save
'
Else
'
' do Stuff for SaveAs
'
End If
End Sub
 
Upvote 0
Yes, I understand how it works else. My question is a little more complicated. I need to do two actions (macro) on SaveAs event:
One with the old name/path of the file, and another with the new name of the file. How do I show VBA the difference in this moments when?
 
Upvote 0
Set an object to the workbook, or extract it's details, in the Workbook_BeforeSave event and use a global variable or document property to keep the the saveas state. Then in the Workbook_Aftersave event you can check the saveas state and make other changes as required to the old file accessing the object (or reopening it from the file details) and to the new Workbook using the ThisWorkbook reference.
 
Upvote 0
Great! Thnx! Workbook_Aftersave did the job. Didn't know it existed.
Made a variable on BeforeSave event and checked for it on the AfterSave event:
Code:
Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    Call Sub1
    If SaveAsUI Then
        SaveAS_Check = True
    End If
End Sub

Sub Workbook_AfterSave(ByVal Success As Boolean)
    If SaveAS_Check = True Then
        Call Sub2
        SaveAS_Check = False
    End If
End Sub
 
Upvote 0
Solution
You're welcome.

I'm glad you got a solution that works for you.
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,876
Members
452,363
Latest member
merico17

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