Which event trigger to call for macro?

albertc30

Well-known Member
Joined
May 7, 2012
Messages
1,091
Office Version
  1. 2019
Platform
  1. Windows
Hi all.

I was wondering how to optimize this spreadsheet I have to which I must save with a diferent name every week and then run macros to clear all data in the new saved sheet.

How would one go about running my macro when the current file is saved as but so that the macro will run on the new file saved and NOT on the old one as data must remain intact.

Regards and many thanks.
Albert
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
If you want the user to be able to name the workbook maybe try adding something like this to the end of the macro which gives a prompt with the days date and your file name:

Code:
 Application.Dialogs(xlDialogSaveAs).Show Format(Date, "mm-dd-yyyy") & " Your File Name"

It may be beneficial to use these lines of code with the above save prompt. They will set the drive and directory the dialog opens to:

Code:
ChDrive ("M")
ChDir ("M:\Your Directory\Your Directory\")
If you want to save the workbook as an .xlsm use this:
Code:
FileNameVal = Application.GetSaveAsFilename(, "Excel Macro-Enabled Workbook (*.xlsm), *.xlsm")
ThisWorkbook.SaveAs Filename:=FileNameVal & ".xlsm", FileFormat:=xlOpenXMLWorkbookMacroEnabled

If you want to simply save the workbook use something like this:

Code:
ThisWorkbook.Save

If you want to use an event in conjunction with your macro then it would be the BeforeSave Event. Hope this helps.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,920
Messages
6,122,279
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