Excel file with login id and password - VBA code
How to create an excel file with the login id and password with the help of VBA code?
Sometimes we need security to keep our data safe. So Only I have got an idea for excel to write VBA code to create a login id and password window. In this way, we can keep our excel file safe. I know we do have another option to create the password to open the file and that is simple. There we will not get a beautiful window to enter login id and password.
Okay, let us start.
First, open one excel workbook and save it as Excel Macro-Enabled Workbook.
Next Open the visual basic developer tab ( short-cut = ALT+11)
Click on ThisWorkbook, go to Object, and from dropdown select Workbook. Then go to Procedure and from dropdown select Active. ( See the below image for better understanding.)
Now you need to write the code inside the workbook active sub.
Note: userform1 name is the default name when you are creating the first userform. In case you have renamed your userform1 to some different name then change the code accordingly.
The VBA code is:-
Private Sub Workbook_Activate()
Application.Visible = False
UserForm1.Show
End Sub
Now insert userform from the Insert menu from the developer tab. Design your userform.
- Use 2 labels - One for Login ID and another for Password
- Use 2 TextBox - One to enter login id detail and another for password
- TextBox 1 - rename it as tblogin
- TextBox2 - rename it as tbpassword - Make sure you choose * in the passwordchar field from the properties. So that when you will enter the password then it will show *** instead of password characters.
- Use 2 Buttons - One for reset TextBoxes and another Login to excel file.
- commandbutton1 - rename it as cbreset
- commandbutton2 - rename it as cblogin
- Use 2 Images - This is just to make your form attractive.
Now you need to write code for LOGIN button. So double-click on the LOGIN button and start writing code. Next, double-click on the RESET button and start writing code.
VBA code for login id and password is here.
Private Sub cblogin_Click()
If tblogin.Text = "ckmehta" And tbpassword.Text = "12345" Then
MsgBox "You have logined successfully"
Application.Visible = True
Unload Me
Else
MsgBox "Please enter correct login id or password"
tblogin = ""
tbpassword = ""
End If
End Sub
Private Sub cbreset_Click()
tblogin = ""
tbpassword = ""
MsgBox "All reset"
End Sub
In case you are going on my path then you can simply copy and paste my code and save your file. The code will work.
Make sure you will change these 2 fields
- tblogin.Text = "ckmehta" - change ckmehta to a different login id name per your need.
- tbpassword.Text = "12345" - change 12345 to a different password per your need.
to open the file
login id is: ckmehta
password is: 12345
-----
You can change the login id and password in the VBA code per your requirement.
-----
Thank You
No comments