Access password blocked sheet - Excel - excel

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:

Related

The Input Box keeps popping up in excel

I am trying to type code in VBA where a macro button pops up based on a cell value and the password typed after a cell has that specific value. It works for the most part, but the Input Box keeps popping up after I have typed the password. Whenever I click on another cell or type something in a different cell, the input box keeps popping up after I've typed the password for admin. I am not sure why it does that.
Sub enable_()
Application.EnableEvents = True
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
'If user is selected, hide button. If admin is selected, enter password to see the button'
If Range("A5") = "User" Then ActiveSheet.CommandButton1.Visible = False
If Range("A5") = "Admin" Then
strwork = InputBox("Please enter Admin Password.")
If strwork = "excel" Then ActiveSheet.CommandButton1.Visible = True
End If
End Sub
Worksheet Change feat. InputBox
Application.Intersect method
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
' If user is selected, hide button.
' If admin is selected, enter password to see the button'
Dim sCell As Range: Set sCell = Me.Range("A5")
If Intersect(Target, sCell) Is Nothing Then Exit Sub
With Me.CommandButton1
Select Case sCell.Value
Case "User"
If .Visible Then .Visible = False
Case "Admin"
Dim pWord As String
pWord = InputBox("Please enter Admin Password.")
'If .Visible Then .Visible = False
If pWord = "excel" Then
If Not .Visible Then .Visible = True
End If
End Select
End With
End Sub

Hide multiple worksheets and diplay only one worksheet vba

I am trying to secure my workbook, i have multiple sheets that i need to hide and leave only one sheet to be displayed that will have a command button (Picture 1) when I click on it I have a userform that pops up (Picture 2) with username and password to open specific sheets( ive set different usernames and passwords to open specific sheet)
enter image description here
enter image description here
I wrote this code :
Private Sub CommandButton1_Click()
Dim User, Pass As String
User = Me.TextBox1.Text
Pass = Me.TextBox2.Text
If User = "Admin" And Pass = "123" Then
MsgBox ("Bienvenu")
Application.Visible = True
ActiveWorkbook.Unprotect Password:="password"
Sheets("ACCEUIL").Visible = True
Sheets("Liste Personnes").Visible = True
Sheets("Liste IT").Visible = True
Sheets("Liste PE").Visible = True
Sheets("Liste EM").Visible = True
Sheets("Liste ELC").Visible = True
Sheets("Liste Habilitation").Visible = True
Sheets("Liste EPC").Visible = True
Sheets("Liste ECH").Visible = True
ActiveWorkbook.Protect Password:="password"
Unload Me
Else
'Pole Essais
If User = "Admin1" And Pass = "456" Then
MsgBox " Bienvenue"
Application.Visible = True
ActiveWorkbook.Unprotect Password:="password"
Sheets("Liste Personnes").Visible = True
Sheets("Liste IT").Visible = True
Sheets("Liste PE").Visible = True
Sheets("Liste EM").Visible = True
Sheets("Liste ELC").Visible = True
Sheets("Liste Habilitation").Visible = True
Sheets("Liste EPC").Visible = True
Sheets("Liste ECH").Visible = True
Sheets("PICHON Franck").Visible = True
Sheets("MAGNIER Jean-Fran?ois").Visible = True
Sheets("LAPIERRE Louis").Visible = True
Sheets("HOSSAERT Didier").Visible = True
Sheets("DEBEYER Nicolas").Visible = True
Sheets("GARCIA Manuel").Visible = True
Sheets("GIRARD Sunny").Visible = True
Sheets("SICOT Thimot?e").Visible = True
Sheets("BEAUVILLAIN Maxime").Visible = True
Sheets("WATTEZ Eric").Visible = True
Sheets("PROUVOST Thomas").Visible = True
Sheets("PROUVOST Mathieu").Visible = True
Sheets("GARCIA Manuel").Visible = True
ActiveWorkbook.Protect Password:="password"
Unload Me
Else
MsgBox " Verifier le nom d'utilisateur ou le mot de passe"
End If
End If
End Sub
Private Sub CommandButton2_Click()
Unload Me
ActiveWorkbook.Close True
End Sub
`
it doesnt work as I want the problem is when I close my workbook and reopen it I have the recent sheets That ive been working on displayed on my workbook, and I want the worksheet "ACCEUIL" to be displayed not those Ive been working on the last time I opened the workbook.
thanks #karma it works very well, but I have something to ask, when i open my workbook i have a userform that pops up to edit/ read my database content when I finish working on my database and close the userform (UF_Choix_Service) i have all the worksheets visible again :/ but when im about to close the file I see in the background that the sheets have disappeared so your code is working good i just want to know how to hide the sheets after closing the userform.
My code to display the userfom is : (which I placed in a workbook module)
Sub Workbook_Open()
Gestion_Comp
end sub
where
Sub_Gestion_Comp ()
Application.ScreenUpdating = False
Nom_Classeur = ThisWorkbook.Name
mode_edition = False
UF_Choix_Service.Show
Select Case MsgBox("Voulez vous sauvegarder le fichier ?", vbYesNo + vbQuestion, "Sauvegarde du fichier")
Case vbYes
ThisWorkbook.Save
End Select
End Sub
enter image description here
Please have a look at this code,
maybe you can implement it to your needs.
Sub test()
Dim arr() As Variant
'get all sheet name except "Sheet1" to array
For Each sh In Worksheets
If sh.Name = "Sheet1" Then
Else
ReDim Preserve arr(X)
arr(X) = sh.Name
X = X + 1
End If
Next
'hide all the sheet name in that array, so the only not hidden is Sheet1
For Each sh In arr
If Sheets(sh).Visible = True Then Sheets(sh).Visible = False
Next
'show all the sheet name in that array
For Each sh In arr
If Sheets(sh).Visible = False Then Sheets(sh).Visible = True
Next
End Sub
I want the worksheet "ACCEUIL" to be displayed not those Ive been
working on the last time I opened the workbook.
Put your macro on the workbook module, something like this :
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim arr() As Variant
For Each sh In Worksheets
If sh.Name = "ACCEUIL" Then
Else
ReDim Preserve arr(X)
arr(X) = sh.Name
X = X + 1
End If
Next
For Each sh In arr
If Sheets(sh).Visible = True Then Sheets(sh).Visible = False
Next
End Sub
You need to save the workbook before you close it.
That's if I'm not mistaken what you want.
EDIT:
when I finish working on my database and close the userform
(UF_Choix_Service). I just want to know how to hide the sheets after closing the userform
If you want all the sheets hidden but the ACCEUIL sheet, then on the Sub where you have the line : Unload UF_Choix_Service try to put this line :
Unload UF_Choix_Service '---> this is your existing code to close the userform
For Each sh In Worksheets
If sh.Name = "ACCEUIL" Then
else
If Sheets(sh.Name).Visible = True Then Sheets(sh.Name).Visible = False
end if
Next
end sub
After the code unload/close the userform, the code above say :
Loop through all the existing worksheets,
If the sheet name is "ACCEUIL", do nothing
other than ACCEUIL,
if this other sheet is visible then hide it.
Don't forget to remove the sub I ask you to put in the workbook module (BeforeClose).

Activate the last active sheet visible

I currently have a workbook that contains several 'veryhidden' sheets, which only will become visible when the correct password has been entered. However, in the case that the correct password is entered, how do I write a code which activates that sheet that just has become visible, i.e. Excel needs to ignore all the 'veryhidden' ones.
I have tried the code:
Sub MoveNext()
On Error Resume Next
Sheets(ActiveSheet.Index + 1).Activate
If Err.Number <> 0 Then Sheets(1).Activate
End Sub
However, this does not work.
Declare Global variables in separate module
say ,
Global Const g_sPassword As String = "password"
on the next module where you need to code ,do it as below
'mention only sheets that should be visible ,other sheets will be automatically hided
Sub OpenSheet(sSheetName As String)
Dim shtSheetToOpen As Worksheet, shtLoopSheet As Worksheet
Set shtSheetToOpen = ThisWorkbook.Sheets(sSheetName)
Application.ScreenUpdating = False
shtSheetToOpen.Visible = xlSheetVisible
For Each shtLoopSheet In ThisWorkbook.Sheets
If shtLoopSheet.Name <> shtSheetToOpen.Name And shtLoopSheet.Name <> "Sheets to be visible" Then
shtLoopSheet.Visible = xlVeryHidden
End If
Next shtLoopSheet
shtSheetToOpen.Activate
Set shtSheetToOpen = Nothing
Set shtLoopSheet = Nothing
End Sub
' then include the below code
Sub OpenSample()
Dim sUserInput As String
sUserInput = InputBox("Please enter password", "Password Prompt", "")
If sUserInput = g_sPassword Then
MsgBox "Access Granted", vbInformation, "Access"
Call OpenSheet("Sheetsname")
sheetname.Activate
Else
MsgBox "Incorrect Password.Please try again", vbCritical, "Error"
End If
End Sub
how do I write a code which activates that sheet that just has become visible
You can store the sheet that you are unhiding in an object and then activate
For example
Private ws As Worksheet
Sub UnhideCode()
'<~~ Just an example since I do not know how you are unhiding
If pass = "OK" Then
Set ws = Sheet1 '<~~ Change this to the relevant sheet
ws.Visible = xlSheetVisible
End If
End Sub
Sub MoveNext()
If Not ws is Nothing then ws.Activate
End Sub

How to auto-protect individual worksheets in Excel using VBA?

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

Protect a sheet from opening within a workbook

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.

Resources