Executing code to clear data in different cells and input words according to rules specified

Status
Not open for further replies.

TheJay

Active Member
Joined
Nov 12, 2014
Messages
364
Office Version
  1. 2019
Platform
  1. Windows
This is based on another thread, the answer has been given but the code doesn't execute as expected: Specifying multiple sheets in OpenWorkbook

I have a workbook that contains multiple sheets. I want Workbook_Open to execute code that clears cells and inserts specific words into a specific sheet and cell according to the rules specified.

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

Could someone please tell me why this is happening and how to fix it?
 
In future, please do not post the same question multiple times. Per Forum Rules (#12), posts of a duplicate nature will be locked or deleted.

In relation to your question here, I have closed this thread so please continue in the linked thread.
 
Upvote 0
Status
Not open for further replies.

Forum statistics

Threads
1,226,850
Messages
6,193,350
Members
453,790
Latest member
yassinosnoo1

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