Specifying multiple sheets in OpenWorkbook

TheJay

Active Member
Joined
Nov 12, 2014
Messages
364
Office Version
  1. 2019
Platform
  1. Windows
Hello there, I have code that I want to run on two specific (named) sheets. I'm not sure what I need to do.

VBA Code:
Private Sub Workbook_Open()
    Dim sh          As Worksheet
    For Each sh In Worksheets
[B]        If sh.Name = "Property Numbering", "VO Areas" Then[/B]
            sh.Protect UserInterFaceOnly:=True, AllowSorting:=True, AllowFiltering:=True
                Range("C13").Select
                Selection.ClearContents
                Range("C8").Select
                Selection.ClearContents
                Range("B2").Select
                Selection.ClearContents
                Range("B2").Select
        Else
            sh.Protect UserInterFaceOnly:=True
        End If
    Next
End Sub

It doesn't like this:
VBA Code:
[B]If sh.Name = "Property Numbering", "VO Areas" Then[/B]

How do I correct this please?

Thanks.
 
VBA Code:
Private Sub Workbook_Open()
    Dim sh As Worksheet
    For Each sh In Worksheets
        If sh.Name = "Property Numbering" Then
            sh.Protect UserInterFaceOnly:=True, AllowSorting:=True, AllowFiltering:=True
            sh.Range("C13,C8,B2").ClearContents
            sh.Range("B2").Value = "'Property Reference Guide (Click Arrow to Start)"
        ElseIf sh.Name = "VO Areas" Then
            sh.Protect UserInterFaceOnly:=True, AllowSorting:=True, AllowFiltering:=True
            sh.Range("C4").ClearContents
            sh.Range("C4").Value = "'Choose P"
        Else
            sh.Protect UserInterFaceOnly:=True
        End If
    Next
End Sub
 
Upvote 0

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
VBA Code:
Private Sub Workbook_Open()
    Dim sh As Worksheet
    For Each sh In Worksheets
        If sh.Name = "Property Numbering" Then
            sh.Protect UserInterFaceOnly:=True, AllowSorting:=True, AllowFiltering:=True
            sh.Range("C13,C8,B2").ClearContents
            sh.Range("B2").Value = "'Property Reference Guide (Click Arrow to Start)"
        ElseIf sh.Name = "VO Areas" Then
            sh.Protect UserInterFaceOnly:=True, AllowSorting:=True, AllowFiltering:=True
            sh.Range("C4").ClearContents
            sh.Range("C4").Value = "'Choose P"
        Else
            sh.Protect UserInterFaceOnly:=True
        End If
    Next
End Sub
Thank you, seems to be working now I have removed the reference to the merged cell:

VBA Code:
        Private Sub Workbook_Open()
Dim sh As Worksheet
For Each sh In Worksheets
If sh.Name = "Property Numbering" Then
sh.Protect UserInterFaceOnly:=True, AllowSorting:=True, AllowFiltering:=True
sh.Range("C13,C8").ClearContents
sh.Range("B2").Value = "'Property Reference Guide (Click Arrow to Start)"
ElseIf sh.Name = "VO Areas" Then
sh.Protect UserInterFaceOnly:=True, AllowSorting:=True, AllowFiltering:=True
sh.Range("C4").ClearContents
sh.Range("C4").Value = "'Choose P"
Else
sh.Protect UserInterFaceOnly:=True
End If
Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,627
Messages
6,120,610
Members
448,973
Latest member
ChristineC

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