Opening worksheets using worksheet names in a named range

anythinggoes3949

New Member
Joined
Feb 8, 2024
Messages
3
Office Version
  1. 2013
Platform
  1. Windows
Hello, I've been writing fairly simple macros for a few years and can usually find what I need in the forums. This time though I'm completely stumped.

I have workbook containing multiple worksheets. This includes a a worksheet called "Master" which contains a list of a select few of the worksheet names found within the same workbook. I've set this list as a named range called "Worksheet_Names".

I would like the macro to loop through the list of worksheet names, open each worksheet in the list in turn and password protect these worksheets. I don't want to password protect every sheet in the workbook, only those listed within this range. If I can get this to work, then I'll set up another macro to unprotect the same sheets.

I'm finding the code gets stuck (debug error) at the 'Sheets(Range(" & cell & ").Value).Activate' line of code so I'm assuming I've done something wrong here. Any help would be greatly appreciated. This is the first time I've posted here so apologies if I've done anything wrong within my post.

Sub Protect_Sheets()
'
Sheets("Master").Activate
Dim cell As Range
For Each cell In ActiveSheet.Range("Worksheet_Names")
Sheets(Range(" & cell & ").Value).Activate
ActiveSheet.Protect "Password1234"
Next cell

End Sub
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
This shall do the job:

VBA Code:
sub do_protect()
Dim cell As Range
For Each cell In Sheets("Master").Range("Worksheet_Names")
  Sheets(cell.Value).Protect "Password1234"
Next cell
end sub
Note no activation of sheet is called. It would make screen flicker. If for some reason you need it then consuder adding
VBA Code:
Application.ScreenUpdating = False
at the beginning of the code and switch it to True at the end of the code
 
Upvote 0
Solution
Try this:

VBA Code:
Sub Protect_Sheets()
   Dim Worksheet_Names As Range
   Set Worksheet_Names = Sheets("Master").Range("Worksheet_Names")
   Dim cell As Range
   For Each cell In Worksheet_Names
      Sheets(cell.Value).Protect "Password1234"
   Next cell
End Sub
 
Upvote 0
This shall do the job:

VBA Code:
sub do_protect()
Dim cell As Range
For Each cell In Sheets("Master").Range("Worksheet_Names")
  Sheets(cell.Value).Protect "Password1234"
Next cell
end sub
Note no activation of sheet is called. It would make screen flicker. If for some reason you need it then consuder adding
VBA Code:
Application.ScreenUpdating = False
at the beginning of the code and switch it to True at the end of the code
I'll give this a try, thank you!
 
Upvote 0
Try this:

VBA Code:
Sub Protect_Sheets()
   Dim Worksheet_Names As Range
   Set Worksheet_Names = Sheets("Master").Range("Worksheet_Names")
   Dim cell As Range
   For Each cell In Worksheet_Names
      Sheets(cell.Value).Protect "Password1234"
   Next cell
End Sub
I'll give this a try, thank you!
 
Upvote 0

Forum statistics

Threads
1,223,897
Messages
6,175,271
Members
452,628
Latest member
dd2

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