How to Login Automatically into Website Using Excel VBA

Alex wants to know how to login into a website automatically using Excel VBA. His email is reproduced below:
‘Hi Mr.Takyar,

Thank you for providing such valuable tutorials for excel.

I was wondering if there is a way this task can be accomplished.

I’m trying to access a website (URL) that requires username and password.

I have to enter my credentials every time I access this site. Is there a way to create a macro or VBA that I can run to make the process quicker? If so can you please demonstrate how I can get this accomplished. I sincerely appreciate your help.


He wants to automate the process of logging into a website automatically using Excel VBA. He has the username and password for the website. Our video demonstrates how you can login automatically into a website like Gmail using Excel VBA:
1. Go to the Gmail website
2. View its source code to find out the labels used for the username and password
3. Start the Visual Basic Editor in MS Excel
4. Activate the following two items by clicking on Tools and then ‘References…’:
•Microsoft Internet Controls
•Microsoft HTML Object Liabrary
5. Next define two public variables
6. Now start writing the macro
7. Using a looping process access the username and password text-boxes automatically for entry of the relevant data
8. Then identify the ‘submit’ button, click on it and exit the for loop
9. To avoid error messages popping up write a few lines of code to ignore the errors
You’ll notice that the VBA code given below works well:

Dim HTMLDoc As HTMLDocument
Dim MyBrowser As InternetExplorer
Sub MyGmail()

Dim MyHTML_Element As IHTMLElement
Dim MyURL As String
On Error GoTo Err_Clear
MyURL = “”
Set MyBrowser = New InternetExplorer
MyBrowser.Silent = True
MyBrowser.navigate MyURL
MyBrowser.Visible = True
Loop Until MyBrowser.readyState = READYSTATE_COMPLETE
Set HTMLDoc = MyBrowser.document
HTMLDoc.all.Email.Value = “” ‘Enter your email id here
HTMLDoc.all.passwd.Value = “abc+123” ‘Enter your password here
For Each MyHTML_Element In HTMLDoc.getElementsByTagName(“input”)
If MyHTML_Element.Type = “submit” Then MyHTML_Element.Click: Exit For
If Err <> 0 Then
Resume Next
End If
End Sub

Watch the video:

20 thoughts on “How to Login Automatically into Website Using Excel VBA

      1. Nuzul

        Can i know which path Shell (“C:\Program Files (x86)\Mozilla Firefox\firefox.exe should i input it?

        This one?
        Dim MyBrowser As InternetExplorer
        Set MyBrowser = New InternetExplorer

  1. Shilpi

    Hi Sir, I am trying to run this for my Gmail ID; it works all good till putting in the email and password. But, it stops there, doesn’t automatically click on the ‘sign in’. It is very urgent sir, I am working on a project that needs to be submitted in 4 days. Please help.

  2. Terry Thornton

    How do we use this to convert a worksheet to a html file and then up load it to our web site?

    Thank you Dinesh.

  3. Glen

    Dear sir
    It shows a compile error
    the below lines of code is highlighted in “red” when i paste it please help
    MyURL = “”
    HTMLDoc.all.Email.Value = “” ‘Enter your email id here
    HTMLDoc.all.passwd.Value = “abc123″ ‘Enter your password here

      1. Ritesh

        This is because of the “” used in the code. Please remove the “” written in code and replace it manually in your system. The code has the “” in other format. I hope this will help u.

  4. Mahendra

    How can i browse and login to a website using firefox, in IE it is working fine but i want it in Mozilla as Application is compatible with mozilla.. Please help here

  5. Felix Falconi

    Nice code !!
    But I can do to open in url than email. (other non-email)
    for example this page,

    or other blogs

    I guest
    I need username and password.
    In the Excel would have to A2: the url, B2: user name, C2: key (this because eventually can change it). I click on a command button and automatically open my page. As I can do this with Excel macro code?
    Thank you

  6. Pingback: ユーザー(USER) 水で貼る結露防止シート(45×180cm 2枚組) U-225 スノー 【送料無料】【02P30M

  7. Pingback: How to follow multiple hyperlinks and extract webpage data | Excel VBA Training Videos

  8. Vikas Verma

    Hi Thanks for creating such useful macros and I need a micro for auto web login and I was try this code but unfortunately its not working its showing error at time of Compile in bellow mention line.
    HTMLDoc.all.Email.Value = “” ‘Enter your email id here
    HTMLDoc.all.passwd.Value = “abc+123” ‘Enter your password here

    I think my system is not recognize (Email) function so please suggest in this error msg.

    I really appreciate your effort.

    Thank You SO much.

    1. Vikas Verma

      Hi Sir,

      I need your help everything is working fine in above code but its not submitting for login so need your help its Opening Website – Entering ID & Password but its not clicking on login button and I was also try by website source code so please help me out.

      Thanks in Advance 🙂


Leave a Reply

Your email address will not be published. Required fields are marked *