Run VBA code before file is saved

kiwikiki718

Board Regular
Joined
Apr 7, 2017
Messages
80
Office Version
  1. 365
Platform
  1. Windows
hello I have the below code that I want to run before the workbook is saved. but nothing happens when I press save.

VBA Code:
Option Explicit

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

If Worksheets("TUESDAY").Range("A3") = 0 Or Worksheets("TUESDAY").Range("B3") = 0 Then
Worksheets("TUESDAY").Range("A3:B3").Value = Worksheets("MONDAY").Range("A3:B3").Value

End If

If Worksheets("TUESDAY").Range("A13") = 0 Or Worksheets("TUESDAY").Range("B13") = 0 Then
Worksheets("TUESDAY").Range("A13:B13").Value = Worksheets("MONDAY").Range("A13:B13").Value

End If

If Worksheets("TUESDAY").Range("A23") = 0 Or Worksheets("TUESDAY").Range("B23") = 0 Then
Worksheets("TUESDAY").Range("A23:B23").Value = Worksheets("MONDAY").Range("A23:B23").Value
End If

If Worksheets("TUESDAY").Range("A33") = 0 Or Worksheets("TUESDAY").Range("B33") = 0 Then
Worksheets("TUESDAY").Range("A33:B33").Value = Worksheets("MONDAY").Range("A33:B33").Value
End If

If Worksheets("TUESDAY").Range("A43") = 0 Or Worksheets("TUESDAY").Range("B43") = 0 Then
Worksheets("TUESDAY").Range("A43:B43").Value = Worksheets("MONDAY").Range("A43:B43").Value

End If

End sub
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
If it runs, then something happens. If it runs and doesn't do what you expect, that is a different matter. So the question is, does it run? If you don't know for sure then but a breakpoint in the code, or add a msgbox line before the first If block, or write Stop there instead. Then save the wb. If it doesn't suspend the code or raise a message box then you might have disabled events at some point and it never got re-enabled.
 
Upvote 0
I ran your code without modification and it worked for me.
 
Upvote 0

Forum statistics

Threads
1,214,950
Messages
6,122,428
Members
449,083
Latest member
Ava19

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