Macro to open view with a password

J

julief

I am trying to have a macro open a view on a worksheet and really want to
have the worksheet protected. If i set a worksheet password the user is
prompted for it when they click on the macro button, is there any way i can
inlcude the password in the macro? I would also like to protect the
worksheet once the macro has been run.

The macro is as follows.

ActiveSheet.Unprotect
ActiveWorkbook.CustomViews("2 - Monday View").Show
ActiveSheet.Protect DrawingObjects:=True, Contents:=True,
Scenarios:=True _
, AllowFormattingCells:=True, AllowFormattingColumns:=True, _
AllowFormattingRows:=True, AllowSorting:=True
Range("L9").Select
End Sub
 
R

ryguy7272

Here is an example:

Make Sheet1 VeryHidden:
Hit Alt+F11, go to Properties > Visible 2-xlSheetVeryHidden

Place this code in Module1:
Sub Passwrd()
Dim i_pwd As String

i_pwd = InputBox("Please Enter Password to Unhide Sheet", "Unhide Sheet...")
If i_pwd = "" Then
Exit Sub
End If

'#1
Select Case (i_pwd)
Case Is = "Sheet1"
Worksheets("Sheet1").Visible = True
Sheets("Sheet1").Select

Case Else
MsgBox "Incorrect password; no action taken.", vbInformation, _
"Unhide Sheet..."
End Select

Exit Sub

Place this code in the Sheet named ‘ThisWorkbook’ (hit Alt+F11 to see this
object):
Private Sub Workbook_Open()
Module1.Passwrd
End Sub

Good luck,
Ryan---
 
R

ryguy7272

Here is an example:

Make Sheet1 VeryHidden:
Hit Alt+F11, go to Properties > Visible 2-xlSheetVeryHidden

Place this code in Module1:
Sub Passwrd()
Dim i_pwd As String

i_pwd = InputBox("Please Enter Password to Unhide Sheet", "Unhide Sheet...")
If i_pwd = "" Then
Exit Sub
End If

'#1
Select Case (i_pwd)
Case Is = "Sheet1"
Worksheets("Sheet1").Visible = True
Sheets("Sheet1").Select

Case Else
MsgBox "Incorrect password; no action taken.", vbInformation, _
"Unhide Sheet..."
End Select

Exit Sub

Place this code in the Sheet named ‘ThisWorkbook’ (hit Alt+F11 to see this
object):
Private Sub Workbook_Open()
Module1.Passwrd
End Sub

Good luck,
Ryan---
 
P

Per Jessen

Hi

Sure you can include a password in the code. Look at this:

MyPassword = "JustMe"
ActiveSheet.Unprotect Password:=MyPassword
ActiveWorkbook.CustomViews("2 - Monday View").Show
ActiveSheet.Protect Password:=MyPassword, DrawingObjects:=True, _
Contents:=True, Scenarios:=True, AllowFormattingCells:=True, _
AllowFormattingColumns:=True, AllowFormattingRows:=True,
AllowSorting:=True
Range("L9").Select

Regards,
Per
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top