VBA: Run Macro When Sheet Changes

BIGTONE559

Active Member
Joined
Apr 20, 2011
Messages
336
I currently have code that runs when it's selected however, i would like for the code to run once the sheet is changed.

e.g. when the activesheet is Sheet(1) i would like for it to run once the tab for Sheet(2) is pressed. . . as of now. . the code runs once the sheet is selected. e.g. Sheet(1) is the active sheet when Sheet(2) is clicked it runs on sheet(2).

code:
Code:
Private Sub Workbook_SheetDeactivate(ByVal Sh As Object)
Dim aSh As Worksheet
Dim aShRange As Range
Set aSh = ActiveSheet

Set aShRange = aSh.Range("C3")

If Not IsNumeric(Application.Match(Sh.Name, Array("HExport"), 0)) Then

    If aShRange.Value = "" Then
    aSh.Name = aSh.Index
    Else
    aSh.Name = aShRange.Value
    End If
End If


End Sub


As always thanks

Cheers!
 
Try

Code:
Private Sub Workbook_SheetDeactivate(ByVal Sh As Object)
Dim aShRange As Range
Set aShRange = Sh.Range("C3")
If Not IsNumeric(Application.Match(Sh.Name, Array("HExport"), 0)) Then
    If aShRange.Value = "" Then
    aSh.Name = aSh.Index
    Else
    Sh.Name = aShRange.Value
    End If
End If
End Sub

Perfect. What change did you make? it worked perfectly!
 
Upvote 0

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
WAIT!!! i just noticed something trippy. . .

Once i click sheet(2) it does change sheet(1) to the appropriate name. However, If Sheet 2 does not have a value in C3. And i Click Sheet(1) it changes the name of Sheet(1) to the index. If i do have a value in Sheet(2) and i click Sheet(1) it keeps the value that is in Sheet(1).Range(C3)
 
Upvote 0
WAIT!!! i just noticed something trippy. . .

Once i click sheet(2) it does change sheet(1) to the appropriate name. However, If Sheet 2 does not have a value in C3. And i Click Sheet(1) it changes the name of Sheet(1) to the index. If i do have a value in Sheet(2) and i click Sheet(1) it keeps the value that is in Sheet(1).Range(C3)

bump
 
Upvote 0

Forum statistics

Threads
1,224,516
Messages
6,179,231
Members
452,898
Latest member
Capolavoro009

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