ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   macro on Sheet activation (https://www.excelbanter.com/excel-programming/338576-macro-sheet-activation.html)

[email protected]

macro on Sheet activation
 
I have a macro I would like to run each time I switch to a specific
sheet. How can I set this up?

Thanks.


Ian

macro on Sheet activation
 
Assign the code to Worksheet Activate

eg This macro toggles A1 between 0 & 1 each time the sheet is activated

Private Sub Worksheet_Activate()
If Range("A1").Value = 0 Then
Range("A1").Value = 1
Else
Range("A1").Value = 0
End If
End Sub

--
Ian
--
wrote in message
oups.com...
I have a macro I would like to run each time I switch to a specific
sheet. How can I set this up?

Thanks.




[email protected]

macro on Sheet activation
 
So, to be clear, if I change the name to Worksheet_Activate, this will
run on each activation. If instead, I need to set the worksheet
activate event to point to my macro, that is what I need to know how
to do.

Thanks.


Ian

macro on Sheet activation
 
If you rigth click on the sheet tab and click View Code, this will open VBA
editor. In the left pane, double click on the sheet you want to run the code
in then, above the code window select Worksheet in the LH drop-down and
Activate in the RH one. This will create the Worksheet_Activate subroutine
framework (1st & last lines of my code). This is where your code needs to
go. I don't know exactly where your code will be, but it could well be in a
module which will be listed in the LH pane. If not you can use EditFind to
locate the macro name (search in Current Project). Just cut and paste it.

--
Ian
--
wrote in message
oups.com...
So, to be clear, if I change the name to Worksheet_Activate, this will
run on each activation. If instead, I need to set the worksheet
activate event to point to my macro, that is what I need to know how
to do.

Thanks.





All times are GMT +1. The time now is 08:05 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com