Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Enter code for sheet name in VB in excel

Status
Not open for further replies.

treckor

MIS
Jun 16, 2003
1
US
I need to setup a macro that will set the password as the sheet name. Here is my "simplistic" code to do this:

Sub ProtectSheetBack()
'
' ProtectSheetBack Macro
'
' Keyboard Shortcut: Ctrl+p
'
Dim sht As Worksheet
Dim Pswrd As String

For Each sht In ActiveWorkbook.Worksheets

Pswrd = sht.Name
sht.Protect Paswrd


Next sht
End Sub

The sht.Protect Paswrd is not working correctly. May I please have some assistance?

thanks!
nanc
 





Hi,

Correct the variable name.

HINT: Use Option Explicit to FORCE variable declarations.

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 





Hi,

Correct the variable name.

TIP: Use Option Explicit to FORCE variable declarations.

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
I agree, you should always use "Option Explicit"

Code:
Option Explicit
Sub abc()
 Dim intCounter as integer
 etc.
End Sub

To make this automatically happen you can do the following:

While in the vba editor Tools > Options click the Editor tab and check the box for "Require Variable Declaration".

Now future worksheets with macros will always have the Option Explicit declaration



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top