Macro to work before saving only when certain worksheets are active

Pauljj

Well-known Member
Joined
Mar 28, 2004
Messages
2,047
I have created a macro which works before the document is saved, it all works fine but if I wanted to modify it, so it only happened before saving, when certain worksheets were active, how could I modify this code to do that please ?
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Below code will displays all
"workbook - worksheet"

probably u can build logic to run through the list and check of your sheets ...

Code:
For Each ws In Application.Worksheets
    MsgBox ws.Parent.Name & " - " & ws.Name
Next
 
Upvote 0
Do you mean something like this?

<font face=Courier New><br><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> Workbook_BeforeSave(<SPAN style="color:#00007F">ByVal</SPAN> SaveAsUI <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Boolean</SPAN>, Cancel As <SPAN style="color:#00007F">Boolean</SPAN>)<br>    <SPAN style="color:#00007F">Dim</SPAN> SheetsToActOn()<br>    <SPAN style="color:#00007F">Dim</SPAN> x<br>    <br>    SheetsToActOn = Array("Sheet1", "Sheet3") <SPAN style="color:#007F00">'<- Adjust to suit</SPAN><br>    <br>    x = Application.Match(ActiveSheet.Name, SheetsToActOn, 0)<br>    <SPAN style="color:#00007F">If</SPAN> IsNumeric(x) <SPAN style="color:#00007F">Then</SPAN><br>        <SPAN style="color:#007F00">'Your action code goes here</SPAN><br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br></FONT>
 
Upvote 0
Just so you don't think I am being lazy, I have this working....as you can see I wrote it, as its not very clever but I have a feeling its not the best way of writing the code ?

Private Sub WORKBOOK_BEFORESAVE(ByVal SAVEASUI As Boolean, Cancel As Boolean)

If ActiveSheet.Name = "Sunday" Or ActiveSheet.Name = "Monday" Or ActiveSheet.Name = "Tuesday" Or ActiveSheet.Name = "Wednesday" Or ActiveSheet.Name = "Thursday" Or ActiveSheet.Name = "Friday" Or ActiveSheet.Name = "Saturday" Then

ActiveWorkbook.ResetColors

End If

End Sub
 
Upvote 0
Just so you don't think I am being lazy, I have this working....as you can see I wrote it, as its not very clever but I have a feeling its not the best way of writing the code ?

Private Sub WORKBOOK_BEFORESAVE(ByVal SAVEASUI As Boolean, Cancel As Boolean)

If ActiveSheet.Name = "Sunday" Or ActiveSheet.Name = "Monday" Or ActiveSheet.Name = "Tuesday" Or ActiveSheet.Name = "Wednesday" Or ActiveSheet.Name = "Thursday" Or ActiveSheet.Name = "Friday" Or ActiveSheet.Name = "Saturday" Then

ActiveWorkbook.ResetColors

End If

End Sub
If your code works and it isn't slow, then there's nothing really wrong with it.

However, putting those worksheet names into the array in my code should tidy the code a little.
 
Upvote 0
Just one further thing. If the worksheets in the workbook are fixed and the list of sheets to NOT act on is much shorter, that list could be provided instead, and the match logic reversed. Something like this:

<font face=Courier New><br><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> Workbook_BeforeSave(<SPAN style="color:#00007F">ByVal</SPAN> SaveAsUI <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Boolean</SPAN>, Cancel As <SPAN style="color:#00007F">Boolean</SPAN>)<br>    <SPAN style="color:#00007F">Dim</SPAN> DontActSheets()<br>    <SPAN style="color:#00007F">Dim</SPAN> x<br>    <br>    DontActSheets = Array("abc", "def") <SPAN style="color:#007F00">'<- Adjust to suit</SPAN><br>    <br>    x = Application.Match(ActiveSheet.Name, DontActSheets, 0)<br>    <SPAN style="color:#00007F">If</SPAN> IsError(x) <SPAN style="color:#00007F">Then</SPAN><br>        <SPAN style="color:#007F00">'Your action code goes here</SPAN><br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br></FONT>
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,722
Members
452,939
Latest member
WCrawford

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