If you are new to VBA programing you may want to pick up a book or to on how to program in Excel using VBA. I don't really think this forum is really for teaching users from Step 1. You need to become a little familiar with user forms and their event procedures and in some cases especially the sequence of the operation. In any case, I have listed some code for you use. I want to caution you on two things. Even with this code in place, a somewhat savy user will be able to gain access to the workbook without being one of the authorized users. Also, please take caution when placing this code in you workbook. I strongly suggest that you make a sample or test workbook to play around with and keep the original workbook untouched until you are thoroughly satisfied that the code works the way you want it t. Then you can export the code modules and user modules from the test book and then import them into the original workbook.
Create a new workbook with a minimum of 4 worksheets. One of them MUST be named "Sheet4". In Sheet 4, leave cell A1 & B1 blank for now. We'll use your ID later here. Starting in cell A2 type in the names of the other users ( A2, A3, A4 & A5 ) along with their passwords in B2, B3, B4 & B5. Switch to code view (Alt. F11). Place the following code in a standard module:
Code:
Option Explicit
Sub Auto_Open()
UserForm1.Show
End Sub
Sub Auto_Close()
With ActiveWorkbook
.Sheets("Sheet4").Visible = False
.Protect "XXX", True, True
.Save
.Close
End With
End Sub
Public Sub CloseWorkbook()
Call Auto_Close
End Sub
Create a new userform and add the following controls:
- Add (2) labels and change their captions to "UserID" and "Password".
- Add (2) text boxes. Change their names to "txt1" and "txt2".
- Add (2) command buttons. Change their captions to "Login" & "Cancel".
Code:
Option Explicit
Dim Users(4, 1) As String
Dim LoginValid As Boolean
Private Sub CommandButton1_Click()
Dim X As Integer
Dim iCounter As Integer
Start:
If iCounter > 2 Then GoTo SubExit
For X = 0 To UBound(Users, 1)
If txt1.Text = Users(X, 0) And txt2.Text = Users(X, 1) Then
LoginValid = True
GoTo SubExit
End If
Next X
iCounter = iCounter + 1
If MsgBox("Try Again?", vbYesNo + vbCritical, "Login Failed") = vbYes Then
GoTo Start
End If
SubExit:
Unload Me
End Sub
Private Sub UserForm_Initialize()
Dim X As Integer
' Read UserNames & ID's
' Save in Array
For X = 0 To 4
Users(X, 0) = Sheets("Sheet4").Cells(X + 1, 1)
Users(X, 1) = Sheets("Sheet4").Cells(X + 1, 2)
Next X
LoginValid = False
End Sub
Private Sub UserForm_Terminate()
If LoginValid = False Then
Call CloseWorkbook
Else
ActiveWorkbook.Unprotect "XXX"
End If
End Sub
Save the workbook, close the workbook and re-open it. Leave both the User ID and the Password blank, click OK and you should see the workbook. Note that in the code, I set the password to protect the workbook as XXX, and also unprotect the workbook in code. The Auto_Open subroutine runs automatically, each time the workbook is opened, starting the login dialog. The auto_clos subroutine automatically sets the password to XXX, saves to workbook and then closes the workbook. If the login fails, the workbook will close. After you done playing with the code and understand how it works, you can start experimenting with some alterations to customize it for you specific needs. Also, don't forget to add your Username & Password after you have played around with it for a while.
Bookmarks