EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home VBA VBA Resources VBA Input & Output Functions VBA Login
 

VBA Login

Ashwani Jaiswal
Article byAshwani Jaiswal
Madhuri Thakur
Reviewed byMadhuri Thakur

VBA Login

Excel VBA Login Form

VBA Login is a unique way to protect excel workbook and worksheets. Although we have options of Protect Sheet and protect workbook which is available in Review menu option, which we have the fix patterns of locking the worksheet and workbook. With the help of the VBA login, we have created a customize login box by which we can create dialog box or login form in which we can authorize the login id and password to access the complete excel workbook and any specific worksheets. For better understanding, suppose we have an Excel file that has multiple sheets and each sheet has some confidential data to be protected. And each data is meant for everyone, so we can create a login form by giving every user their login id and password and user will only be able to open and access the data which is meant for him. This could be done only by VBA Login.

 

 

How to Generate Login User Form in VBA?

We will learn how to generate logic user form in Excel by using the VBA Code.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

You can download this VBA Login Excel Template here – VBA Login Excel Template

Example #1

In this example, we will learn how to create login box using User form. For this, follow the below steps:

Step 1: For this open VBA and from the Insert menu tab select UserForm.

VBA Login Example 1

Step 2: Now once we get the UserForm opened, from the toolbox select the Label option and create a label box in Userform.

VBA Login Example 1-2

Step 3: At the left hand side we have a Properties of the Label which we have created. We can change the name, font, size, and color of created label.

VBA Login Example 1-3

Step 4: As you can see in below screenshot, we have change the name user form as Label1, Font as Arial Narrow, Fore Color as Blue and Caption as Enter User Name. We can further customize it.

VBA Login Example 1-4

Step 5: Now we will create a text box here to enter the user name for login as shown below.

VBA Login Example 1-5

Step 6: Now we would insert a label for entering the password.

VBA Login Example 1-6

Step 7: Again we would change the name of Label, color, font, size, and name as required below.

Properties window Example 1-7

Step 8: Insert a text box for entering the password against the Password label.

Text Box Example 1-8

Step 9: And last, create a login button with the help of the Command button to click on it.

Command button Example 1-9

Step 10: Now edit the properties of created Command Button from the Properties window at left.

Properties window Example 1-10

Step 11: We are done with part of creating a login form in VBA. Now we will write a small VBA code to make this login work. For this, double click anywhere on created UserForm and in that write the sub-procedure as shown below.

Code:

Private Sub Login_Click()

End Sub

VBA Login Example 1-11

Step 12: Open an IF – End IF loop and write the condition as if the Login flag is FALSE then activate the worksheet and then show login.

Code:

Private Sub Login_Click()

If LoginFlag = False Then
Worksheets("Sheet1").Activate
LogIn.Show
End If

End Sub

IF – End IF loop Example 1-12

Step 13: Now we will compile the code by pressing the F8 key and And run the code by pressing the Play button located below the menu bar, we would get the Login box.

VBA Login Example 1-13

Example #2

Although there are various ways to create a VBA Login. But we will be seeing another code where we will be using IF – End IF loop to list the login. For this, again follow the same process to create a UserForm which we have seen in example-1. Include 2 labels and 2 text boxes, each for entering User Name and Password.

Step 1: We can use the same User Form which we have seen in example-1 as there is no change in the pattern of login box or create a new one. Now double click on User form and write the sub procedure. Now in the open the IF – End IF loop for the condition, if Username box is blank then give me the message as “Enter Login Credentials”, else message “Enter Username”.

Code:

Private Sub Login_Click()

If UsernameBox.Value = "" Then
If PasswordBox.Value = "" Then
MsgBox "Enter Login Credentials!"
Else
MsgBox "Enter Username"
End If

End Sub

VBA Login Example 2-1

Step 2: Now define the Else If condition again, if username value is USER1 and password is Blank then give me the message as “Enter Password” and if the password is “abcd” then hide the application or else give the message as “Re-Enter Login Credentials.”

Code:

Private Sub Login_Click()

If UsernameBox.Value = "" Then
If PasswordBox.Value = "" Then
MsgBox "Enter Login Credentials!"
Else
MsgBox "Enter Username"
End If
ElseIf UsernameBox.Value = "USER1" Then
If PasswordBox.Value = "" Then
MsgBox "Enter Password"
ElseIf PasswordBox.Value = "abcd" Then
Me.Hide: Application.Visible = True
Else
MsgBox "Please Re-Enter Login Credentials."
End If

End Sub

Else If condition Example 2-2

Step 3: At last list the final IF condition for if the login password is incorrect or blank, then return the message “Enter Correct Credentials”.

Code:

Private Sub Login_Click()

If UsernameBox.Value = "" Then
If PasswordBox.Value = "" Then
MsgBox "Enter Login Credentials!"
Else
MsgBox "Enter Username"
End If
ElseIf UsernameBox.Value = "USER1" Then
If PasswordBox.Value = "" Then
MsgBox "Enter Password"
ElseIf PasswordBox.Value = "abcd" Then
Me.Hide: Application.Visible = True
Else
MsgBox "Please Re-Enter Login Credentials."
End If
Else
If PasswordBox.Value = "" Then
MsgBox "Enter Password"
Else
MsgBox "Please Enter Correct Credentials."
End If
End If

End Sub

VBA Login Example 2-3

Step 4: Open a new sub procedure for Command Button which we created for Login, and write the below line for quitting the application.

Code:

Private Sub CommandButton2_Click()

ThisWorkbook.Application.Quit

End Sub

CommandButton2 Example 2-4

Step 5: And at last, write the final sub procedure for Created UserForm name to close the application.

Code:

Private Sub LoginArena_QueryClose(Cancel As Integer, CloseMode As Integer)

ThisWorkbook.Application.Quit

End Sub

Quit Application Example 2-5

Step 6: Final would look like this below.

VBA Login Example 2-6

Step 7: Now compile the each and every step of the code and run it then. In the popped up login form, Enter the user name as USER1 and the password as “abcd” in designated section as per code. If we press ENTER or click on LOGIN, then the code will be exited and we will be returned to the VBA window.

VBA Login Example 2-7

Pros & Cons of VBA Login

  • VBA Login allows different users to login in a different way in a single Excel worksheet.
  • We restrict the user to the data which is meant for him/ her.
  • With the help of VBA Login, we can customize the excel workbook and give the different login user id and password to different users to access the data.
  • Because of lengthy lines of code, the process of creating VBA Login is quite complicated.

Things to Remember

  • We can use Protect Worksheet and Protect Workbook which also consider the password for login and accessing the data. But the password would be the same for each login. Whereas VBA Login allows use to create multiple user id and password.
  • Always create a simple user form first, because when we customize the data the size and attributes of created shapes and boxes, we may end of complicating the coding part.
  • We can create VBA login to direct the user to his login area after login.
  • Once coding is done, save the Excel in macro-enabled excel format.

Recommended Articles

This is a guide to the VBA Login. Here we discuss how to Generate Login User Form in Excel VBA along with practical examples and downloadable excel template. You can also go through our other suggested articles –

  1. VBA Name Worksheet
  2. VBA Input
  3. VBA Solver
  4. VBA Selecting Range

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Web Development & many more.

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA

Download VBA Login Excel Template

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW