Run a macho when closing the Excel file

Lccs

New Member
Joined
Apr 26, 2021
Messages
5
Office Version
  1. 2013
Platform
  1. Windows
Hi everyone!

I have this macro and I want to run it when the user closes the file. How can I put it running automatically?

VBA Code:
Private Sub Workbook_BeforeClose(ByVal SaveAsUI As Boolean, Cancel As Boolean)

    last_row_ose = get_last_row("S1", "I")
    Worksheets("S1").Range("I12:O" & last_row_ose).ClearContents
    
    last_row_pa = get_last_row("S2", "C")
    Worksheets("S2").Range("C9:G" & last_row_pa).ClearContents
    
    last_row_pro = get_last_row("S3", "C")
    Worksheets("S3").Range("C7:AZ" & last_row_pro).ClearContents
    
    last_row_pr = get_last_row("S4", "C")
    Worksheets("S4").Range("C8:AZ" & last_row_pr).ClearContents
    
    last_row_to = get_last_row("S5", "C")
    Worksheets("S5").Range("C6:H" & last_row_to).ClearContents
          
End Sub

Thanks in advance for your help.
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Welcome to the Board!

It will run automatically before closing, provided that:
1. You place this in the "ThisWorkbook" module (it MUST be in this module to run automatically)
2. Macros/VBA are enabled

You may also want to add a "Save" command to the end of that code, so those changes are saved!
(There is not much point in making changes before closing the file if you are not saving those changes).

Just add a line before the "End Sub" line like:
VBA Code:
ActiveWorkBook.Save
 
Upvote 0
Welcome to the Board!

It will run automatically before closing, provided that:
1. You place this in the "ThisWorkbook" module (it MUST be in this module to run automatically)
2. Macros/VBA are enabled

You may also want to add a "Save" command to the end of that code, so those changes are saved!
(There is not much point in making changes before closing the file if you are not saving those changes).

Just add a line before the "End Sub" line like:
VBA Code:
ActiveWorkBook.Save
Thank you so much for your help! :)

I had no idea that only in the "ThisWorkbook" module it would be possible to run stuff automatically
 
Upvote 0
You are welcome.
I had no idea that only in the "ThisWorkbook" module it would be possible to run stuff automatically
That is not quite the whole story.

VBA code that runs automatically is called "Event Procedure" code. The code runs automatically upon some "event" happening, like the closing of a workbook or the updating of certain cells.
There are two types of event procedure code:
1. Workbook level, which are found in the "ThisWorkbook" module.
2. Worksheet level, which are found in each "Sheet" module.

In order for it to happen automatically, not only must be it placed in one of those places, it also MUST be named a certain way (you CANNOT name the procedure anything you like).
Any code placed in a Standard/General will not run automatically, no matter what you call it.
 
Upvote 0
You are welcome.

That is not quite the whole story.

VBA code that runs automatically is called "Event Procedure" code. The code runs automatically upon some "event" happening, like the closing of a workbook or the updating of certain cells.
There are two types of event procedure code:
1. Workbook level, which are found in the "ThisWorkbook" module.
2. Worksheet level, which are found in each "Sheet" module.

In order for it to happen automatically, not only must be it placed in one of those places, it also MUST be named a certain way (you CANNOT name the procedure anything you like).
Any code placed in a Standard/General will not run automatically, no matter what you call it.
I really appreciate your detailed explanation! Thank you! I am new with VBA so I have a lot of things to learn :)
 
Upvote 0
You are quite welcome.
Glad I was able to help.
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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