You are here:

Excel/VBA variables for Worksheet Unprotect


In Excel v2010, I have a workbook with 30 worksheets, each named after a sales rep (firstname lastname) e.g. Bob Smith.  Each sheet is protected with a password; I'm declaring variables in a module for both the username (which is the same as the sheet name) and password for the sheet.
The username variables are U1, U2, etc.  
The password variables are Up1, Up2, etc.
The following example is just for the 1st 2 sheets:

Public Const U1 As String = "Bob Smith"
Public Const U2 As String = "Linda Jones"
Public Const Up1 As String = "BS"  'password for Bob Smith
Public Const Up2 As String = "LJ"  'password for Linda Jones

Currently I have code that will unprotect each sheet, e.g.
Worksheets(U1).Unprotect Up1
Worksheets(U2).Unprotect Up2

While that code works, I'd much rather do this in a For Next loop; something like the following:
Dim x as string, y as string
For i = 1 to 30
x = "U"&i
y = "Up"&i
Worksheets(x).Unprotect y

But of course that doesn't work.
Obviously I'm doing something stupid; can you tell me what I'm doing wrong.
Many thanks for your expertise.

Hi Al,
you can use the Excel VBA Evaluate function. But it requires evaluating another function or sub as per below. No need to declare your constants but you need to add to the custom function below your own list of names and passwords. Example below is for the two sheets you mentioned.

Function WSN(x, ispwd As Boolean)
Select Case x
   Case 1
       WSN = IIf(ispwd, "BS", "Bob Smith")
   Case 2
       WSN = IIf(ispwd, "LJ", "Linda Jones")
   Case Else
' do nothing
End Select
End Function

Sub DoIt()

For i = 1 To 2

   x = Application.Evaluate("WSN(" & i & ", false)")
   y = Application.Evaluate("WSN(" & i & ", true)")
   Worksheets(x).Unprotect y
Next i

End Sub

IF your passwords are generated as the capitalized initials of your worksheet names, then an easier solution is:

Sub DoIt2()

For Each sh In Worksheets
pwd = UCase(Left(sh.Name, 1) & Mid(sh.Name, InStr(2, sh.Name, " ") + 1, 1))
Worksheets(sh.Name).UnProtect pwd
Next sh

End Sub

If you have names with 3 parts i.e. a middle name and you also use the middle initials, then you could consider using the Split function for the pwd variable in DoIt2.

Hope this helps.
About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here


All Answers

Answers by Expert:

Ask Experts


Gyula Gulyas


I can answer most questions related to macros, worksheet functions, VBA, Office automation (calling/using other MS Office programs) and geocoding using Google Maps. I have 10 years experience using and programming in MS Excel and other office applications. I have extensive experience linking and using DLLs in Excel.


Macros, worksheet functions, VBA, linking of DLLs and Office automation, geocoding using Google Maps.

©2017 All rights reserved.