I've put this code into a worksheet which should make sure individual worksheets are protected with their individual passwords. It seems to work fine but I'm trying to access the worksheets and all of them are saying incorrect password?
Private Sub Workbook_BeforeClose(Cancel As Boolean)
With ThisWorkbook
.Worksheets("2073 NSW").Protect Password = "2073"
.Worksheets("2091 NSW").Protect Password = "2091"
.Worksheets("3105 VIC").Protect Password = "3105"
.Worksheets("3091 VIC").Protect Password = "3091"
.Worksheets("4058 QLD").Protect Password = "4058"
.Worksheets("4091 QLD").Protect Password = "4091"
.Worksheets("6024 WA").Protect Password = "6024"
.Worksheets("6091 WA").Protect Password = "6091"
End With
Application.EnableAnimations = False
ThisWorkbook.Save
Application.EnableEvents = True
End Sub
The problem is inside the With ThisWorkbook line. It should be Password:="2073" not Password = "2073"
You forgot to use the := which used to assign a value to a certain named argument.
Private Sub Workbook_BeforeClose(Cancel As Boolean)
With ThisWorkbook
.Sheets("2073 NSW").Protect Password:="2073"
.Sheets("2091 NSW").Protect Password:="2091"
.Sheets("3105 VIC").Protect Password:="3105"
.Sheets("3091 VIC").Protect Password:="3091"
.Sheets("4058 QLD").Protect Password:="4058"
.Sheets("4091 QLD").Protect Password:="4091"
.Sheets("6024 WA").Protect Password:="6024"
.Sheets("6091 WA").Protect Password:="6091"
End With
Application.EnableAnimations = False
ThisWorkbook.Save
Application.EnableEvents = True
End Sub
Related
The code below restricts access by hiding a sheet unless a password is entered. If it is entered correctly, the sheet can be viewed from the individual tabs. However, it won't let me view and then edit the sheet.
Can this be adjusted to allow the user to enter a password and then view and edit the sheet?
Private Sub Workbook_Open()
Sheets("Sheet1").Visible = xlSheetHidden
End Sub
Public ViewAccess As Boolean 'In restricted sheet's activate event
Private Sub Worksheet_Activate()
If ViewAccess = False Then
Me.Visible = xlSheetHidden
Response = Application.InputBox("Password", xTitleId, "", Type:=2)
If Response = "123" Then
Me.Visible = xlSheetVisible
Application.EnableEvents = True
ViewAccess = True
End If
End If
End Sub
Following code will help you. When a user will select a sheet with name HiddenSheet it will ask for password. If password is correct then it will allow for editing data otherwise will go to previous sheet automatically, You have to change HiddenSheet for your sheet name.
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
Dim MySheetName As String
MySheetName = "HiddenSheet" 'The sheed which I want to hide.
If Application.ActiveSheet.Name = MySheetName Then
Application.EnableEvents = False
Application.ActiveSheet.Visible = False
response = Application.InputBox("Password", "Enter Password", "", Type:=2)
If response = "123456" Then 'Unhide Password.
Application.Sheets(MySheetName).Visible = True
Application.Sheets(MySheetName).Select
End If
End If
Application.Sheets(MySheetName).Visible = True
Application.EnableEvents = True
End Sub
Code snipped:
In my spreadsheet I have a button that is linked to the following VBA:
Sub Button_01()
If UserForm1.checkPassword() = True Then
Sheet1.Range("C3").Value = 1
Else
End If
End Sub
With this button I trigger a user form which has the following VBA:
Private passwordStatus As Boolean
Private Sub CommandButton1_Click()
Dim a As String
Dim Password As String
a = "123"
Password = TextBox1.Text
passwordStatus = False
If Password = a Then
MsgBox "Password Correct.", vbInformation
passwordStatus = True
Unload Me
Else
MsgBox "Password Incorrect. Please try again.", vbCritical
End If
End Sub
Function checkPassword() As Boolean
UserForm1.Show
checkPassword = passwordStatus
End Function
The UserForm1 opens without any issue but when I enter a password into the userform the process does not continue. The UserForm1 just stays there.
I cannot find the mistake in my code that blocks the macro from continuing after I entered the password. Can you help me?
To me your code works perfectly, apart from:
Sub Button_01()
If UserForm1.checkPassword() = True Then
Sheet1.Range("C3").Value = 1
Else
End If
End Sub
This piece of code will error out.
Change:
Sheet1.Range("C3").Value = 1
To:
Sheets(1).Range("C3").Value = 1
Clicking on a button on the sheet with Button_01 assigned to it will open the userform1.
On the opened userform I can click OptionButton1 and it will run it's click_event
If I have "123" typed in textbox1 it will hide the userform, a wrong password triggers a msgbox to retry.
Passing information between a userform and a regular module is normally done via public variables. You should put the bulk of your code in the regular module, and only use the userform to pass the check back.
Regular module:
Public CorrectPassword As Boolean
Sub Button_01()
CorrectPassword = False
UserForm1.Show
If CorrectPassword = True Then
Sheets(1).Range("C3").Value = 1
Else
Msgbox "Password Incorrect. Please try again.", vbCritical
End If
End Sub
Userform module:
Private Sub CommandButton1_Click()
Dim a As String
Dim Password As String
a = "123"
Password = TextBox1.Text
If Password = a Then CorrectPassword = True
Unload Me
End Sub
I have an excel workbook with multiple sheets. Is there any way to password protect users from even opening a sheet within the workbook? The sheet has a large graph on it that I don't want everyone to be able to see, let alone edit.
Thanks in advance for your help
EDIT
I used the following code to allow users to click a formcontrol button to access the sheet in question.
Sub ShowHeatMap()
Dim S As String
S = InputBox("Enter Password")
If S = vbNullString Then
Exit Sub
ElseIf S <> "wiretransfer" Then
Exit Sub
Else
Worksheets("Training Heat Map").Visible = xlSheetVisible
End If
End Sub
This is associated with my button on a kind of "homepage" sheet that I added to the workbook.
But I can't get the sheet to remain hidden when you open the workbook again. I tried this code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
ThisWorkbook.Worksheets("Training Heat Map").Visible = xlSheetVeryHidden
End Sub
Any ideas? This code is inputted on the module for the sheet under general declarations
Via this answer, I think this might do the trick for you. Within VBA, put this within ThisWorkbook:
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
Dim MySheets As String, Response As String
MySheet = "Sheet1"
If ActiveSheet.Name = MySheet Then
ActiveSheet.Visible = False
Response = InputBox("Enter password to view sheet")
If Response = "MyPass" Then
Sheets(MySheet).Visible = True
Application.EnableEvents = False
Sheets(MySheet).Select
Application.EnableEvents = True
End If
End If
Sheets(MySheet).Visible = True
End Sub
Obviously will require a bit of tailoring to your needs. Remember that you will also need to password protect your VBA code, otherwise anyone will be able to view it and find out the password.
Is there a way to prevent people from inserting worksheets manually. I have a form that creates the worksheets automatically based on the information the user puts in it. The code I currently uses will prevent people from creating worksheets but it also prevents my form from creating worksheets. Here is what I am using. It is in the workbook module.
Private Sub Workbook_NewSheet(ByVal Sh As Object)
Application.DisplayAlerts = False
Sh.Delete
Application.DisplayAlerts = True
End Sub
Declare this in a Module
Public BoolAdd As Boolean
This in your Workbook_NewSheet
Private Sub Workbook_NewSheet(ByVal Sh As Object)
If BoolAdd = False Then
Application.DisplayAlerts = False
Sh.Delete
Application.DisplayAlerts = True
Else
BoolAdd = False
End If
End Sub
and set the BoolAdd to TRUE in your userform before you add the sheet.
Private Sub CommandButton1_Click()
BoolAdd = True
Sheets.Add
End Sub
Logic: The userform will set the public variable to True. This will ensure that sheet deletion code will not run. Also we have to set it back to false else the user will be able to add the sheet after the userform is closed.
Is there a built-in way in Excel to protect just one spreadsheet in a Workbook with a Password?
Something like when user ticks or selects a Control , a password is prompted before a Worksheet become visible.
If its not built into Excel, can it be implemented with VBA?
You could try this approach which:
Makes the sheet VeryHidden and Protected so that it can't be unprotected - or detected - from the standard xl menus
Adds back in this protection on the Workbook_BeforeClose and Workbook_Open events
I haved used a sheet called YourSheet for this example
You should protect the VBA in this project as well to add further security.
Insert an Active X checkbox and use code to check if:
The checkbox is True
The user knows the password to UnHide the sheet. (Fred is used in this example)
CheckBox code
Private Sub CheckBox1_Click()
Dim StrPass As String
If CheckBox1.Value Then
StrPass = Application.InputBox("Please enter the password", "Admin check")
If StrPass = "fred" Then
With ThisWorkbook.Sheets("YourSheet")
.Unprotect "fred"
.Visible = xlSheetVisible
MsgBox "Sheet unhidden!", vbInformation
End With
Else
MsgBox "wrong password", vbCritical
End If
End If
End Sub
the ThisWorkbook module
Private Sub Workbook_BeforeClose(Cancel As Boolean)
With ThisWorkbook.Sheets("YourSheet")
.Protect "fred"
.Visible = xlVeryHidden
End With
End Sub
Private Sub Workbook_Open()
With ThisWorkbook.Sheets("YourSheet")
.Protect "fred"
.Visible = xlVeryHidden
End With
End Sub