Automatically Run Macro Before Saving

  • I know that there is a 'before save' event that I can use to run a macro whenever I save or save as the file. I've tried the following code but it doesn't appear to work and my VB skills are pretty abysmal. I've tried looking it up using a search (that's where I got this code from) but haven't had much luck getting it to work.Here's the code:

    Private Sub Workbook_ThisWorkbook(ByVal SaveAsUI As Boolean, cancel As Boolean)
    Application.Run "'Data Entry2.xls'!CopyData"
    End Sub

    Thanks for any help with this.

  • Re: Run Macro Before Saving

    Hi Keith,

    You need the sub to be in the Workbook module.

    <Alt +F11> to open the VBE. Double click 'ThisWorkbook' in the Project explorer window (top left window). You will then see General at the top left of the Code window and Declarations at the top right of the Code window.

    Clicking on General drops down 'Workbook' and creates the default Open sub. Select Beforesave from the declarations dropdown and you'll see this:

    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    End Sub



Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!