Excel VBA Login Form
It is possible to create a login-based user form in Excel VBA with all the login ID drop-down lists, and it will share the password separately. However, at some point, you must have an idea of creating a password-based login user form, which requires the user to pick their user ID and enter the password to access the required worksheet.
This article will show you how to create a login UserForm using Excel VBA.

How to Create a Login User Form?
For example, assume you have region-wise sales numbers in different worksheets. We have four different zone names, and each zone worksheet has its related data only. Now, the idea is to create a login form where the โEastโ zone sales head should see only โEastโ zone data. Not any other zones, but as an admin, you should see all the zonesโ worksheets.
First, we need to insert a sheet name called โAdmin.โ Then, in this admin sheet, we need to create โLogin IDโ and โPasswordโ credentials.
3
We have named the zone and password the same. You can change this later. We have created the name range for zone names as โZoneList.โ This โName Managerโ will be used later on this login UserForm.

When the user opens the file, they should see one dummy sheet in the background, so create a new sheet and name it the โDummyโ sheet.

Using these worksheets, we will create a login UserForm.
Step 1: Insert User Form
Press ALT + F11 key to open the VBA Editor window.

- From the โInsertโ tab, insert โUserForm.โ

- It will create a new UserForm like the below one.

- Press the F4 key to see the โPropertiesโ window. From this window, change the name of the UserForm to โLogInUF.โ

- Similarly, using this properties window, we can play with the properties of the UserForm. We have made some of the property changes. We can refer to the below properties window to apply changes to the properties of the UserForm.

- Now, my UserForm looks like this.

Step 2: Design Userform
- From the toolbox of the UserForm, insert two label boxes and enter the text, as shown below.

- From the toolbox, insert โCombo Box.โ

- For this ComboBox excel, we need to get the zone names from the worksheet โAdmin Sheet,โ so from the properties window of the โCombo Box,โ first give a name to this combo box as โZone_List_ComboBoxโ under the โNameโ property.

- From the โRowSourceโ property of the combo box, enter the name given to the zone list in the โAdmin Sheet.โ

- Now, our ComboBox should show zone names in its dropdown list in excel.

- For โEnter Your Password,โ we need to insert a โText Boxโ from the toolbox.

- For this โText Box,โ we need to change the โNameโ property and change it as โPassword_TB.โ

Now in the coding for the VBA login form, โCombo Boxโ will be referred to by the name โZone_List_ComboBox,โ and โText Boxโ will be referred to by the name โPassword_TB.โ
- Insert two โCommandButtonโ and enter the text as โLog Inโ and โLog Out.โ

For the โLog Inโ command button, change the name property to โLogin_CommandButton,โ and for the โLog Outโ command button, change the name property to โLogOut_CommandButton.โ
Step 3: Code
We completed the VBA login UserForm design part. Next, it is time to write the code to create a login-based UserForm in Excel VBA.
- Double click on the โLog Inโ Command Button. It will open a blank sub procedure like the below one.

Inside this procedure, we need to write the code about what should happen if we press the โLog Inโ button.
We have already written the code. We can copy and paste the code from below inside the above procedure.
Code:
Private Sub Login_CommandButton_Click() If Zone_List_ComboBox.Value = “” Then MsgBox “Zone Cannot be Blank!!!”, vbInformation, “Zone Name” Exit Sub End If If Password_TB.Value = “” Then MsgBox “Password Cannot be Blank!!!”, vbInformation, “Password” Exit Sub End If If Zone_List_ComboBox.Value = “Admin” And Password_TB.Value = “Admin” Then Unload Me Dim Ws As Worksheet For Each Ws In ActiveWorkbook.Worksheets Ws.Visible = xlSheetVisible Next Ws Sheets(“Admin”).Select Else Dim ZoneName As String Dim Password As Variant ZoneName = Zone_List_ComboBox.Value Password = Application.WorksheetFunction.VLookup(ZoneName, Sheets(“Admin”).Range(“A:B”), 2, 0) If Password <> Password_TB.Value Then MsgBox “Password is not matching”, vbInformation, “Wrong Password” Exit Sub End If If Password = Password_TB.Value Then Unload Me Sheets(ZoneName).Visible = True Sheets(ZoneName).Select ActiveSheet.Range(“A1”).Select End If End If End Sub
Similarly, double click on the โLog Outโ command button and enter the below code.
Code:
Private Sub LogOut_CommandButton_Click() ย ย ย ย
ย ย ย ย ย ThisWorkbook.Save ย ย ย ย
ย ย ย ย ย ThisWorkbook.Close
End Sub

Now, double click on โUserFormโ (not on any of the buttons inserted) and add the below code.
Code:
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
. ย ‘ Prevents use of the Close button
ย ย ย ย ย If CloseMode = vbFormControlMenu Then
ย ย ย ย ย ย ย ย ย MsgBox “Clicking the Close button does not work.”
ย ย ย ย ย ย ย ย ย Cancel = True
ย ย ย ย ย End If
End Sub

- Now, double-click on the โThisWorkbookโ icon. It will open up below the blank code field.

Now add the below code in this blank field.
Code:
Private Sub Workbook_Open()
ย ย ย ย Dim Ws As Worksheet
ย ย ย ย Application.DisplayAlerts = False
ย ย ย ย Application.ScreenUpdating = False
ย ย ย ย For Each Ws In ActiveWorkbook.Worksheets
ย ย ย ย If Ws.Name <> “Dummy” Then
ย ย ย ย Ws.Visible = xlSheetVeryHidden
ย ย ย ย End If
ย ย ย ย Next Ws
ย ย ย ย LogInUF.Show
End Sub

- We have completed the coding part of the form. Next, save the workbook as a โMacro-Enabledโ workbook and reopen the workbook. Upon reopening the workbook, we should see the below window.

- We must choose the respective zone name from this UserForm and enter the associated password. For example, we will choose the โEastโ zone from the dropdown and enter the password.

- If we click โLog In,โ we can see only the โEastโ zone worksheet.

Similarly, if we log in with โAdmin,โ we can access all the worksheets.

Like this, we can create login-based password-protected worksheet access.
Things to Remember
- It would help if we used the same names given to the UserForm, text box, command button, and combo box in the coding.
- We can change the zone name and password according to your wish.
Recommended Articles
This article has been a guide to VBA Login. Here, we discuss how to create a login form in Excel VBA with the help of an example and a downloadable Excel sheet. You can learn more about VBA from the following articles: –