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.
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
VBA Code:
Private Sub Workbook_Open()
    Dim sh         As Worksheet
    For Each sh In Worksheets
        If sh.Name = "Property Numbering" Or sh.Name = "VO Areas" Then
            sh.Protect UserInterFaceOnly:=True, AllowSorting:=True, AllowFiltering:=True
            sh.Range("C13,C8,B2").ClearContents
        Else
            sh.Protect UserInterFaceOnly:=True
        End If
    Next
End Sub

BTW, you don't need to select cells in order to clear their contents.
 
Upvote 0
Thank you, that's very informative. How about keeping the formatting rules to these two sheets but clearing different cells in different sheets?
"Property Numbering"
sh.Range("C13,C8,B2").ClearContents

"VO Areas"
sh.Range("C4").ClearContents
 
Upvote 0
Thank you, that's very informative. How about keeping the formatting rules to these two sheets but clearing different cells in different sheets?
"Property Numbering"
sh.Range("C13,C8,B2").ClearContents

"VO Areas"
sh.Range("C4").ClearContents
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
        ElseIf sh.Name = "VO Areas" Then
            sh.Protect UserInterFaceOnly:=True, AllowSorting:=True, AllowFiltering:=True
            sh.Range("C4").ClearContents
        Else
            sh.Protect UserInterFaceOnly:=True
        End If
    Next
End Sub
 
Upvote 0
Okay, encountered unexpected behavior with this code.

It doesn't like a range to be specified when one of the cells includes a merge, so had to go back to specifying individual.

Then, I found that all the code was being executed on the second specified sheet and nothing on the first. So, it cleared data from a list on the VO Areas based on

VBA Code:
   Range("C13").Select
            Selection.ClearContents
            Range("C8").Select
            Selection.ClearContents
            Range("B2").Select
            Selection.ClearContents

Then added "Property Reference Guide (Click Arrow to Start)" to cell B2 based on

VBA Code:
            Range("B2").Value = "'Property Reference Guide (Click Arrow to Start)"
            Range("B2").Select

It then executed the expected code:
VBA Code:
    ElseIf sh.Name = "VO Areas" Then
            sh.Protect UserInterFaceOnly:=True, AllowSorting:=True, AllowFiltering:=True
            sh.Range("C4").ClearContents
            Range("C4").Value = "'Choose P"
            Range("C4").Select

Overall code:

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
            Range("C13").Select
            Selection.ClearContents
            Range("C8").Select
            Selection.ClearContents
            Range("B2").Select
            Selection.ClearContents
            Range("B2").Value = "'Property Reference Guide (Click Arrow to Start)"
            Range("B2").Select
        ElseIf sh.Name = "VO Areas" Then
            sh.Protect UserInterFaceOnly:=True, AllowSorting:=True, AllowFiltering:=True
            sh.Range("C4").ClearContents
            Range("C4").Value = "'Choose P"
            Range("C4").Select
        Else
            sh.Protect UserInterFaceOnly:=True
        End If
    Next
End Sub
 
Upvote 0
Does anyone have any ideas how to only execute the code on the specified page and prevent it all from running on the visible sheet instead?
 
Upvote 0
I've unmarked this as resolved because of the bug, could someone please get back to me? Thanks.
 
Upvote 0
I am confused as to what the issue is . If you can rephrase what you want to happen while giving specific details ie:- sheets, cells etc ...
 
Upvote 0
Thanks for your reply.

The workbook has multiple worksheets. The first is called Property Numbering and the second is called VO Areas. The rest are irrelevant.

I want the following to run on Property Numbering:

VBA Code:
            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").Value = "'Property Reference Guide (Click Arrow to Start)"
            Range("B2").Select

I want the following to run on VO Areas:

VBA Code:
            sh.Protect UserInterFaceOnly:=True, AllowSorting:=True, AllowFiltering:=True
            sh.Range("C4").ClearContents
            Range("C4").Value = "'Choose P"
            Range("C4").Select

All unspecified sheets should run:

VBA Code:
sh.Protect UserInterFaceOnly:=True
 
Upvote 0

Forum statistics

Threads
1,215,365
Messages
6,124,513
Members
449,168
Latest member
CheerfulWalker

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