Problem With Excel Coding

Genoseeker

New Member
Joined
Dec 9, 2011
Messages
3
Here is what i need.

I have an Excel workbook with 15 different sheets and 5 different users need to see multiple sheets but not all of them.

Is there a way to program it in VBA code so that it prompts you for a password when you open the file, and based on the password it makes visible the sheets allowed for that password?

I found
Private Sub Worksheet_Activate()
ActiveWindow.WindowState = xlMinimized
If InputBox("Enter Password for this sheet") <> "ABC" Then Sheets("OkToSeeThisOne").Activate
ActiveWindow.WindowState = xlMaximized
End Sub

Using google but its not enough for what i need. Can someone help me or point me in the right direction. I have no clue.
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Genoseeker,

I am happy to give you some code that will do what you want.
Can you respond to the following.

If you use an input box for the password then the password is entered as readable text. Is that ok? To have * type disguised text it is necessary to use an activex textbox.

Do you have any other code manipulating these sheets in any way?

Will the visible sheets combination be different for each of your 5 users and will the combinations be fixed or is there a need to easily change the combinations?

Tony
 
Upvote 0
Genoseeker,

I am happy to give you some code that will do what you want.
Can you respond to the following.

If you use an input box for the password then the password is entered as readable text. Is that ok? To have * type disguised text it is necessary to use an activex textbox.

Do you have any other code manipulating these sheets in any way?

Will the visible sheets combination be different for each of your 5 users and will the combinations be fixed or is there a need to easily change the combinations?

Tony

1. password shows is ok
2. no other code.
3. yes for example password 1 will see sheets 1-15 (admin). password 2 will see sheets 1-4, password 3 will see 5-8 and so on. as long as i get some code i can try to google/learn my way to customize the code for me.
4. once combinations are set no need to change them.
 
Last edited:
Upvote 0
Genoseeker, Peter,

If you check out Peter's link you will see that it utilises input boxes to receive usernames and passwords before making a specific sheet visible to each valid user.
I offer below an alternative that allows an administrator to control and vary, which of the sheets are to be visible to each user. Password entry is made by way of a textbox on a user form. This type of textbox allows for the disguise of password characters by using *****.

I believe that this soution provides pretty reasonable security given that Excel and vba can never be considered truly secure.

I have included only password entry, rather than username and password, but that could easily be added.

It might be that if neither this nor Peter's solution, give you exactly what you require, the answer is some combination of the two.

You will require a sheet named Login. Not critical but make it the leftmost of your sheet tabs.
Typically, set the sheet out as below....

Login

<TABLE style="BACKGROUND-COLOR: #ffffff; PADDING-LEFT: 2pt; PADDING-RIGHT: 2pt; FONT-FAMILY: Arial,Arial; FONT-SIZE: 10pt" border=1 cellSpacing=0 cellPadding=0><COLGROUP><COL style="WIDTH: 30px; FONT-WEIGHT: bold"><COL style="WIDTH: 129px"><COL style="WIDTH: 91px"><COL style="WIDTH: 91px"><COL style="WIDTH: 71px"><COL style="WIDTH: 64px"><COL style="WIDTH: 64px"><COL style="WIDTH: 64px"></COLGROUP><TBODY><TR style="TEXT-ALIGN: center; BACKGROUND-COLOR: #cacaca; FONT-SIZE: 8pt; FONT-WEIGHT: bold"><TD></TD><TD>A</TD><TD>B</TD><TD>C</TD><TD>D</TD><TD>E</TD><TD>F</TD><TD>G</TD></TR><TR style="HEIGHT: 17px"><TD style="TEXT-ALIGN: center; BACKGROUND-COLOR: #cacaca; FONT-SIZE: 8pt">1</TD><TD style="TEXT-ALIGN: left" colSpan=7>Enter ALL worksheet names below, C3 to ?3, without leaving a blank in the range.</TD></TR><TR style="HEIGHT: 17px"><TD style="TEXT-ALIGN: center; BACKGROUND-COLOR: #cacaca; FONT-SIZE: 8pt">2</TD><TD style="TEXT-ALIGN: left" colSpan=7>Enter "Y" in appropriate cells to allow user access.</TD></TR><TR style="HEIGHT: 17px"><TD style="TEXT-ALIGN: center; BACKGROUND-COLOR: #cacaca; FONT-SIZE: 8pt">3</TD><TD style="BACKGROUND-COLOR: #c0c0c0">User</TD><TD style="BACKGROUND-COLOR: #c0c0c0">Password</TD><TD style="BACKGROUND-COLOR: #c0c0c0">Login</TD><TD style="BACKGROUND-COLOR: #c0c0c0">Sheet1</TD><TD style="BACKGROUND-COLOR: #c0c0c0">Sheet2</TD><TD style="BACKGROUND-COLOR: #c0c0c0">Sheet3</TD><TD style="BACKGROUND-COLOR: #c0c0c0">Sheet4</TD></TR><TR style="HEIGHT: 17px"><TD style="TEXT-ALIGN: center; BACKGROUND-COLOR: #cacaca; FONT-SIZE: 8pt">4</TD><TD style="BACKGROUND-COLOR: #ffff99">Administrator</TD><TD style="BACKGROUND-COLOR: #ffff99">Admin</TD><TD style="TEXT-ALIGN: center; BACKGROUND-COLOR: #ffff99">Y</TD><TD style="TEXT-ALIGN: center; BACKGROUND-COLOR: #ffff99">Y</TD><TD style="TEXT-ALIGN: center; BACKGROUND-COLOR: #ffff99">Y</TD><TD style="TEXT-ALIGN: center; BACKGROUND-COLOR: #ffff99">Y</TD><TD style="TEXT-ALIGN: center; BACKGROUND-COLOR: #ffff99">Y</TD></TR><TR style="HEIGHT: 17px"><TD style="TEXT-ALIGN: center; BACKGROUND-COLOR: #cacaca; FONT-SIZE: 8pt">5</TD><TD>Tom</TD><TD>PW1</TD><TD style="BACKGROUND-COLOR: #c0c0c0"></TD><TD style="TEXT-ALIGN: center">Y</TD><TD style="TEXT-ALIGN: center">Y</TD><TD></TD><TD></TD></TR><TR style="HEIGHT: 17px"><TD style="TEXT-ALIGN: center; BACKGROUND-COLOR: #cacaca; FONT-SIZE: 8pt">6</TD><TD>****</TD><TD>PW2</TD><TD style="BACKGROUND-COLOR: #c0c0c0"></TD><TD></TD><TD></TD><TD style="TEXT-ALIGN: center">Y</TD><TD></TD></TR><TR style="HEIGHT: 17px"><TD style="TEXT-ALIGN: center; BACKGROUND-COLOR: #cacaca; FONT-SIZE: 8pt">7</TD><TD>Mary</TD><TD>PW3</TD><TD style="BACKGROUND-COLOR: #c0c0c0"></TD><TD></TD><TD style="TEXT-ALIGN: center">Y</TD><TD></TD><TD></TD></TR><TR style="HEIGHT: 17px"><TD style="TEXT-ALIGN: center; BACKGROUND-COLOR: #cacaca; FONT-SIZE: 8pt">8</TD><TD></TD><TD></TD><TD></TD><TD></TD><TD></TD><TD></TD><TD></TD></TR></TBODY></TABLE>

Excel tables to the web >> http://www.excel-jeanie-html.de/index.php?f=1" target="_blank"> Excel Jeanie HTML 4

** Administrator must be Row 4 and is the only user that will have access to Login sheeet

You will then need to go to the VBE and insert a new Form module.

Add a textbox
Add 2 labels for the instructional texts. One above and one below the textbox.
Add 2 control buttons at the bottom of the form.

Using the Proerties pane, edit /set the following properties for the form objects.
Form - Rename as frmLogin
Label1 - Edit caption to read 'Please enter your password'
Label2 - Edit caption to read 'Passwords are case sensitive'
Label1 & Label2 - Font - Size to suit, say 10

Textbox1 - Rename as tbPWord
PasswordChar enter *
Font - set to suit

CommandButton1 - Rename as cbCancel
Edit Caption to 'Cancel'
CommandButton2 - Rename as cbEnter
Edit Caption to 'Enter'
Change the Default property to True
That last setting Default = True will allow the user to hit Enter key and still trigger the cbEnter button code.

*****
As and when I can figure out how to do it I will upload an image of the form!!!! Any clues will be gratefully received.
*****

You will then need to add the following code.
ALL OF WHICH CAN BE COPIED AND PASTED


In the ThisWorkbook module....

Code:
Private Sub Workbook_Open()
On Error GoTo Out
Sheets("Login").Visible = xlSheetVisible
For Each sht In Sheets
  If Not sht.Name = "Login" Then sht.Visible = xlVeryHidden
Next sht
ActiveWindow.DisplayWorkbookTabs = False
ActiveWindow.WindowState = xlMinimized
Application.EnableCancelKey = xlDisabled
frmLogin.Show
Out:
On Error GoTo 0
End Sub

In the frmLogin module.......

Code:
'Note:  Declare  this variable in the general area at the top of the module
Public Tries As Integer
 
 
 
Private Sub cbCancel_Click()
Unload Me
ThisWorkbook.Close SaveChanges:=False
End Sub
Private Sub cbEnter_Click()
Application.EnableCancelKey = xlErrorHandler
pW = Me.tbPWord.Value
On Error GoTo BadPW
With Sheets("Login")
pwRow = Application.WorksheetFunction.Match(pW, .Range("B4:B10000"), 0) + 3
On Error GoTo Out
GoTo GoodPW
BadPW:
On Error GoTo Out
Tries = Tries - 1
If Tries = 0 Then
Tries = 3
MsgBox "Access Denied"
Unload Me
GoTo Out
Beep
End If
MsgBox "You have " & Tries & " more attempts to enter a valid password"
Exit Sub
GoTo Out
GoodPW:
On Error Resume Next
Tries = 3
NumCol = Sheets.Count + 2
VisShts = 0
For c = 3 To NumCol
myShtName = .Cells(3, c).Value
If .Cells(pwRow, c).Value = "Y" Then
Sheets(myShtName).Visible = xlSheetVisible
VisShts = VisShts + 1
End If
Next c
 
Unload Me
If Not VisShts > 0 Then GoTo Out
Sheets("Login").Visible = xlVeryHidden
ActiveWindow.WindowState = xlMaximized
ActiveWindow.DisplayWorkbookTabs = True
If pwRow = 4 Then
Ans = MsgBox("Do you wish access the Login sheet?", vbYesNo)
If Ans = vbYes Then Sheets("Login").Visible = xlSheetVisible
End If
On Error GoTo 0
Exit Sub
Out:
On Error GoTo 0
End With
Unload Me
ThisWorkbook.Close SaveChanges:=False
End Sub
 
 
Private Sub UserForm_Activate()
Tries = 3
End Sub
 
 
 
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
'Prevents user from closing form with X
If CloseMode = vbaFormControlMenu Then
Cancel = True
MsgBox "Please use the Cancel button!"
End If
End Sub

Unless I have overlooked anything, that should be good to go.

Obviously, you will need to make sure that each use'rs instance of Excel has the appropriate sttings for Macro security.

The only thing that I would add to this is perhaps password protecting the VB Editor so that security cannot be easily compromised via the vba route.

I do hope that that helps.
Please let me know if it does or if you find any errors or omissions.

Tony
 
Last edited:
Upvote 0
How can I edit this code and add a feature to prompt for username in addition to password? I tried this and I don't know why, but it keeps saying I have the wrong password even though I am logged into the computer as administrator and using the Admin password. Help please. I really want to use this for some financial spreadsheets of ours that we only want certain office staff to be able to see certain tabs/worksheets. Thank you.


Genoseeker, Peter,

If you check out Peter's link you will see that it utilises input boxes to receive usernames and passwords before making a specific sheet visible to each valid user.
I offer below an alternative that allows an administrator to control and vary, which of the sheets are to be visible to each user. Password entry is made by way of a textbox on a user form. This type of textbox allows for the disguise of password characters by using *****.

I believe that this soution provides pretty reasonable security given that Excel and vba can never be considered truly secure.

I have included only password entry, rather than username and password, but that could easily be added.

It might be that if neither this nor Peter's solution, give you exactly what you require, the answer is some combination of the two.

You will require a sheet named Login. Not critical but make it the leftmost of your sheet tabs.
Typically, set the sheet out as below....

Login

ABCDEFG
TomPW1
****PW2
MaryPW3

<colgroup><col style="WIDTH: 30px; FONT-WEIGHT: bold"><col style="WIDTH: 129px"><col style="WIDTH: 91px"><col style="WIDTH: 91px"><col style="WIDTH: 71px"><col style="WIDTH: 64px"><col style="WIDTH: 64px"><col style="WIDTH: 64px"></colgroup><tbody>
[TD="bgcolor: #cacaca, align: center"]1[/TD]
[TD="colspan: 7, align: left"]Enter ALL worksheet names below, C3 to ?3, without leaving a blank in the range.[/TD]

[TD="bgcolor: #cacaca, align: center"]2[/TD]
[TD="colspan: 7, align: left"]Enter "Y" in appropriate cells to allow user access.[/TD]

[TD="bgcolor: #cacaca, align: center"]3[/TD]
[TD="bgcolor: #c0c0c0"]User[/TD]
[TD="bgcolor: #c0c0c0"]Password[/TD]
[TD="bgcolor: #c0c0c0"]Login[/TD]
[TD="bgcolor: #c0c0c0"]Sheet1[/TD]
[TD="bgcolor: #c0c0c0"]Sheet2[/TD]
[TD="bgcolor: #c0c0c0"]Sheet3[/TD]
[TD="bgcolor: #c0c0c0"]Sheet4[/TD]

[TD="bgcolor: #cacaca, align: center"]4[/TD]
[TD="bgcolor: #ffff99"]Administrator[/TD]
[TD="bgcolor: #ffff99"]Admin[/TD]
[TD="bgcolor: #ffff99, align: center"]Y[/TD]
[TD="bgcolor: #ffff99, align: center"]Y[/TD]
[TD="bgcolor: #ffff99, align: center"]Y[/TD]
[TD="bgcolor: #ffff99, align: center"]Y[/TD]
[TD="bgcolor: #ffff99, align: center"]Y[/TD]

[TD="bgcolor: #cacaca, align: center"]5[/TD]

[TD="bgcolor: #c0c0c0"][/TD]
[TD="align: center"]Y[/TD]
[TD="align: center"]Y[/TD]

[TD="bgcolor: #cacaca, align: center"]6[/TD]

[TD="bgcolor: #c0c0c0"][/TD]

[TD="align: center"]Y[/TD]

[TD="bgcolor: #cacaca, align: center"]7[/TD]

[TD="bgcolor: #c0c0c0"][/TD]

[TD="align: center"]Y[/TD]

[TD="bgcolor: #cacaca, align: center"]8[/TD]

</tbody>


Excel tables to the web >> http://www.excel-jeanie-html.de/index.php?f=1" target="_blank"> Excel Jeanie HTML 4

** Administrator must be Row 4 and is the only user that will have access to Login sheeet

You will then need to go to the VBE and insert a new Form module.

Add a textbox
Add 2 labels for the instructional texts. One above and one below the textbox.
Add 2 control buttons at the bottom of the form.

Using the Proerties pane, edit /set the following properties for the form objects.
Form - Rename as frmLogin
Label1 - Edit caption to read 'Please enter your password'
Label2 - Edit caption to read 'Passwords are case sensitive'
Label1 & Label2 - Font - Size to suit, say 10

Textbox1 - Rename as tbPWord
PasswordChar enter *
Font - set to suit

CommandButton1 - Rename as cbCancel
Edit Caption to 'Cancel'
CommandButton2 - Rename as cbEnter
Edit Caption to 'Enter'
Change the Default property to True
That last setting Default = True will allow the user to hit Enter key and still trigger the cbEnter button code.

*****
As and when I can figure out how to do it I will upload an image of the form!!!! Any clues will be gratefully received.
*****

You will then need to add the following code.
ALL OF WHICH CAN BE COPIED AND PASTED


In the ThisWorkbook module....

Code:
Private Sub Workbook_Open()
On Error GoTo Out
Sheets("Login").Visible = xlSheetVisible
For Each sht In Sheets
  If Not sht.Name = "Login" Then sht.Visible = xlVeryHidden
Next sht
ActiveWindow.DisplayWorkbookTabs = False
ActiveWindow.WindowState = xlMinimized
Application.EnableCancelKey = xlDisabled
frmLogin.Show
Out:
On Error GoTo 0
End Sub

In the frmLogin module.......

Code:
'Note:  Declare  this variable in the general area at the top of the module
Public Tries As Integer
 
 
 
Private Sub cbCancel_Click()
Unload Me
ThisWorkbook.Close SaveChanges:=False
End Sub
Private Sub cbEnter_Click()
Application.EnableCancelKey = xlErrorHandler
pW = Me.tbPWord.Value
On Error GoTo BadPW
With Sheets("Login")
pwRow = Application.WorksheetFunction.Match(pW, .Range("B4:B10000"), 0) + 3
On Error GoTo Out
GoTo GoodPW
BadPW:
On Error GoTo Out
Tries = Tries - 1
If Tries = 0 Then
Tries = 3
MsgBox "Access Denied"
Unload Me
GoTo Out
Beep
End If
MsgBox "You have " & Tries & " more attempts to enter a valid password"
Exit Sub
GoTo Out
GoodPW:
On Error Resume Next
Tries = 3
NumCol = Sheets.Count + 2
VisShts = 0
For c = 3 To NumCol
myShtName = .Cells(3, c).Value
If .Cells(pwRow, c).Value = "Y" Then
Sheets(myShtName).Visible = xlSheetVisible
VisShts = VisShts + 1
End If
Next c
 
Unload Me
If Not VisShts > 0 Then GoTo Out
Sheets("Login").Visible = xlVeryHidden
ActiveWindow.WindowState = xlMaximized
ActiveWindow.DisplayWorkbookTabs = True
If pwRow = 4 Then
Ans = MsgBox("Do you wish access the Login sheet?", vbYesNo)
If Ans = vbYes Then Sheets("Login").Visible = xlSheetVisible
End If
On Error GoTo 0
Exit Sub
Out:
On Error GoTo 0
End With
Unload Me
ThisWorkbook.Close SaveChanges:=False
End Sub
 
 
Private Sub UserForm_Activate()
Tries = 3
End Sub
 
 
 
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
'Prevents user from closing form with X
If CloseMode = vbaFormControlMenu Then
Cancel = True
MsgBox "Please use the Cancel button!"
End If
End Sub

Unless I have overlooked anything, that should be good to go.

Obviously, you will need to make sure that each use'rs instance of Excel has the appropriate sttings for Macro security.

The only thing that I would add to this is perhaps password protecting the VB Editor so that security cannot be easily compromised via the vba route.

I do hope that that helps.
Please let me know if it does or if you find any errors or omissions.

Tony
 
Upvote 0

Forum statistics

Threads
1,223,912
Messages
6,175,340
Members
452,637
Latest member
Ezio2866

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top