macro to not run on certain sheets

bluepenink

Well-known Member
Joined
Dec 21, 2010
Messages
585
Hello

How can i integrate the following macro to not run on the following sheets

Sheet1, Sheet75 (i will be creating more sheets so is it possible to have a macro that I can fill with the sheet# as i create them)

this is my macro

Code:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Excel.Range)
  On Error GoTo Rename_Error
  If Target.Address = "$C$4" And Len(Target.Value2) > 0 Then
    With Target.Offset(, 2)
        .Calculate
        Sh.Name = .Value2
    End With
  End If
  Exit Sub
Rename_Error:
  MsgBox "The sheet name could not be renamed. Please check " & vbCrLf & _
        "that you did not use illegal characters."
End Sub

pls and thx u!!
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Try adding this as the first line of code...

If Sh.Name = "Sheet1" Or Sh.Name = "Sheet75" Then Exit Sub
 
Upvote 0
hello

does this appear correct to you?

Code:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Excel.Range)
  If Sh.Name = "Sheet1" Or Sh.Name = "Sheet6" Or Sh.Name = "Sheet2" Or Sh.Name = "Sheet3" Or Sh.Name = "Sheet4" _
  Or Sh.Name = "Sheet5" Or Sh.Name = "Sheet7" Or Sh.Name = "Sheet8" Or Sh.Name = "Sheet9" Then Exit Sub
  On Error GoTo Rename_Error
  If Target.Address = "$C$5" And Len(Target.Value2) > 0 Then
    With Target.Offset(, 2)
        .Calculate
        Sh.Name = .Value2
    End With
  End If
  Exit Sub
Rename_Error:
  MsgBox "The sheet name could not be renamed. Please check " & vbCrLf & _
        "that you did not use illegal characters."
End Sub
 
Upvote 0
Code:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Excel.Range)
  If Sh.Name = "Sheet1" Or Sh.Name = "Sheet6" Or Sh.Name = "Sheet2" Or Sh.Name = "Sheet3" Or Sh.Name = "Sheet4" _
  Or Sh.Name = "Sheet5" Or Sh.Name = "Sheet7" Or Sh.Name = "Sheet8" Or Sh.Name = "Sheet9" Then Exit Sub
Given that you are talking about that naming system for your sheets and that you are talking about numbers 1 through 9 inclusive, I would probably write it like this instead...

Code:
If Left(Sh.Name, 5) = "Sheet" And Mid(Sh.Name, 6) < 10 Then Exit Sub
However what you wrote looks like it should work as well. The easiest way to see is to actually run the code. As long as you don't save the workbook if something goes wrong, you can't "break" anything.
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,275
Members
452,902
Latest member
Knuddeluff

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