PDA

View Full Version : using inputbox data



fdesorme
03-26-2009, 05:45 AM
I'm new to VBA programming and am trying to get a simple worksheet protection macro working but am having problems with getting the password set using data from an inputbox. The procedure protects the worksheet, but I cannot unprotect the worksheet within excel when using the same password. Here is the code:


Sub ProtectSheet()

Dim Pswd As String
Pswd = InputBox("Please enter the password.", "Password entry")
Dim WS As Worksheet
For Each WS In ThisWorkbook.Worksheets
WS.Protect Password:="& Pswd &", DrawingObjects:=True, Contents:=True, Scenarios:=True _
, AllowSorting:=True, AllowFiltering:=True
Next WS

End Sub


Thanks

GTO
03-26-2009, 06:02 AM
Greetings,

In the above, WS.Protect Password:="& Pswd &" means you are literally assigning "& Pswd &" as the password, regardless of what you typed into the InputBox.

Try WS.Protect Password:=Pswd

Hope this helps,

Mark

fdesorme
03-26-2009, 06:22 AM
It worked perfectly.

Thanks for your help.