This article is mainly focused on creating applications using the Visual Basic for Applications (VBA) programming language. With Excel VBA you can automate tasks in Excel using what is called a macro. Create a simple macro that will be executed after clicking on a command button.

Chapter 1

In this chapter, learn how to turn on the Developer tab. The Developer tab is not visible by default in Excel, we must configure it to show that tab. This tab contains a variety of tools that allows development and customization of Excel macro applications.

This guidance applies to Excel 2010.

  1. Start the Excel application.

  2. On the File tab, click the Options button.

    The following figure shows the File tab and Options button in Excel 2010.

     File tab and Options button in Excel

  3. Choose the Customize Ribbon button in the Excel Options dialog box.

  4. Check the Developer check box.

    The following figure shows the Customize Ribbon in Excel 2010.

    Customize the Ribbon

  5. Click OK to save the selection changes.

Chapter 2

This chapter teaches you how to create a simple login form in Excel VBA. The User form we will create looks as follows.

The following figure shows the simple login form in Excel VBA.

simple login form in Excel VBA

Step 1

To place a command button on your worksheet and assign a macro:

  1. On the Developer tab click Insert.

  2. In the ActiveX Controls group click the Command Button.

    ActiveX Controls group

  3. Drag a Command Button onto your worksheet.

  4. Right-click CommandButton1 and click View code.

    view code

    (Hint: be sure Design Mode is selected.)

  5. On the Insert tab click New User Form.

    click new User Form

  6. Right-click Sheet1 and click View code to write the code between Private Sub CommandButton1_Click () and End Sub.

  7. Add the code line shown below.
    1. Private Sub CommandButton1_Click ()
    2. UserForm1.Show
    3. End Sub

Step 2

Add Controls.

  1. Add the controls listed in the table below. Clicking on control from the Toolbox next you can drag a control on a User Form:

    User Form

    Command Button 1

    I modified some of the properties for that control

    Command Button 1

    TextBox 1

    I modified some of the properties for that control

    Textbox 1

    TextBox 2

    I modified some of the properties for that control

    Textbox 2

    Image 1(Login)

    I modified some of the properties for that control

    Login

    Image 2(Username)

    I modified some of the properties for that control

    Username

    Image 3 (Password)

    I modified some of the properties for that control

    Password

  2. In the Project Explorer, right-click on UserForm1 and then click View Code.

  3. Add the following code lines.
    1. Public Username As String
    2. Public Password As String
    3. Public i As Integer
    4. Public j As Integer
    5. Public u As String
    6. Public p As String
    7. Private Sub CommandButton1_Click ()
    8. Application.ScreenUpdating = False
    9. If Trim (TextBox1.Text) = "" And Trim (TextBox2.Text) = "" Then
    10. MsgBox "Enter username and password.", vbOKOnly
    11. Else If Trim (TextBox1.Text) = "" Then
    12. MsgBox "Enter the username ", vbOKOnly
    13. Else If Trim(TextBox2.Text) = "" Then
    14. MsgBox "Enter the Password ", vbOKOnly
    15. Else
    16. Username = Trim (TextBox1.Text)
    17. Password = Trim (TextBox2.Text)
    18. i = 1
    19. Do While Cells (1, 1).Value <> ""
    20. j = 1
    21. u = Cells (i, j).Value
    22. j = j + 1
    23. p = Cells (i, j).Value
    24. If Username = u And Password = p And Cells (i, 3).Value = "fail" Then
    25. MsgBox "Your Account temporarily locked", vbCritical
    26. Exit Do
    27. Else If Username = u And Password = p Then
    28. Call clear
    29. UserForm1.Hide
    30. UserForm2.Label1.Caption = u
    31. UserForm2.Label1.ForeColor = &H8000000D
    32. UserForm2.Show
    33. Exit Do
    34. Else If Username <> u And Password = p Then
    35. MsgBox "Username not matched", vbCritical + vbOKCancel
    36. Exit Do
    37. Else If Username = u And Password <> p Then
    38. If Cells (i, 3).Value = "fail" Then
    39. MsgBox "Your account is blocked", vbCritical + vbOKCancel
    40. Exit Do
    41. Else If Cells (i, 4).Value < 2 Then
    42. MsgBox "Invalid password", vbCritical
    43. Cells (i, 4).Value = Cells (i, 4) + 1
    44. Exit Do
    45. Else
    46. Cells (i, 4).Value = Cells (i, 4) + 1
    47. Cells (i, 3).Value = "fail"
    48. Cells (i, 2).Interior.ColorIndex = 3
    49. Exit Do
    50. End If
    51. Else
    52. i = i + 1
    53. End If
    54. Loop
    55. End If
    56. Application.ScreenUpdating = True
    57. End Sub
    58. Sub clear ()
    59. TextBox1.Value = ""
    60. TextBox2.Value = ""
    61. End Sub
    62. Private Sub TextBox1_Enter ()
    63. With TextBox1
    64. .Back Color = &H8000000E
    65. .Fore Color = &H80000001
    66. .Border Color = &H8000000D
    67. End With
    68. TextBox1.Text = ""
    69. End Sub
    70. Private Sub TextBox1_AfterUpdate ()
    71. If TextBox1.Value = "" Then
    72. TextBox1.BorderColor = RGB (255, 102, 0)
    73. End If
    74. i = 1
    75. Do Until Is Empty (Cells (i, 1).Value)
    76. If TextBox1.Value = Cells (i, 1).Value Then
    77. With TextBox1
    78. .Border Color = RGB (186, 214, 150)
    79. .Back Color = RGB (216, 241, 211)
    80. .Fore Color = RGB (81, 99, 51)
    81. End With
    82. End If
    83. i = i + 1
    84. Loop
    85. End Sub
    86. Private Sub TextBox2_Enter ()
    87. With TextBox2
    88. .Back Color = &H8000000E
    89. .Fore Color = &H80000001
    90. .Border Color = &H8000000D
    91. End With
    92. TextBox2.Text = ""
    93. End Sub
    94. Private Sub TextBox2_AfterUpdate ()
    95. i = 1
    96. Username = TextBox1.Value
    97. Password = TextBox2.Value
    98. If TextBox2.Text = "" Then
    99. TextBox2.BorderColor = RGB (255, 102, 0)
    100. End If
    101. Do Until Is Empty (Cells (i, 1).Value)
    102. j = 1
    103. u = Cells (i, j).Value
    104. j = j + 1
    105. p = Cells (i, j).Value
    106. If Username = u and Password = p Then
    107. With TextBox2
    108. .Border Color = RGB (186, 214, 150)
    109. .Back Color = RGB (216, 241, 211)
    110. .Fore Color = RGB (81, 99, 51)
    111. End With
    112. Exit Do
    113. Else If Username = u and Password <> p Then
    114. TextBox2.BorderColor = RGB (255, 102, 0)
    115. Exit Do
    116. Else
    117. i = i + 1
    118. End If
    119. Loop
    120. End Sub
    121. Sub settings ()
    122. With UserForm1
    123. TextBox1.ForeColor = &H8000000C
    124. TextBox2.ForeColor = &H8000000C
    125. TextBox1.BackColor = &H80000004
    126. TextBox2.BackColor = &H80000004
    127. TextBox1.Text = "Username"
    128. TextBox2.Text = "Password"
    129. TextBox1.BorderColor = RGB (0, 191, 255)
    130. TextBox2.BorderColor = RGB (0, 191, 255)
    131. CommandButton1.SetFocus
    132. End With
    133. End Sub
    134. Private Sub UserForm_Initialize ()
    135. Call settings
    136. End Sub

Step 3

Test the user form using the following.

  1. Exit the Visual Basic Editor, enter the labels and data shown below into rows.

  2. Deselect the Design mode selection then click Command Button 1 on the sheet.

    Command Button 1 on the sheet

Demos

Demos

The following figure shows the Welcome page after successful login.

welcome page

The following figure shows entering an invalid password.

enter invalid password

The following figure shows login status and login attempt.

login status and login attempt

The following figure shows an account blocked after trying the wrong password 3 times.

account block after trying 3 time

Thanks for reading. I hope this article useful for VBA beginners.

(Hint: Excel source password -> 123987)