DISCOVER
×

How to Send a Username & Password to a Website With Excel VBA Code

Updated April 17, 2017

Excel's programming language, Visual Basic for Applications (VBA), lets Microsoft Office programmers extend the abilities that ship with Excel. One example of this extension is a VBA program that can enter login information on a web page, and then submit that information. This model is a hierarchy of virtual objects that comprise Excel's programmable features. Sending a user name and password to a website with VBA saves your program's user from the inconvenience of entering this information manually.

Open Excel, then display the VBA coding window by pressing "Alt-F11."

Double-click the item displaying "This workbook" in the navigation pane at screen left. This opens a window for entering code for the current workbook.

Type or paste the following program into the window that appears. This program calls the VBA statement "AppActivate" to direct input from the current application, Excel, to your browser. The "SendKeys" statement sends keystrokes, the login and password information, to the activated browser. The text "{TAB}" and "~" represent the "Tab" and "Enter" keys. The statement Application tells Excel to pause briefly to allow Windows and your browser time to process the keystrokes sent to it.

Public Sub send Password()

AppActivate "BROWSER_NAME", True

SendKeys "YOUR_LOGIN_ID", True

Application.Wait 1000

SendKeys "{TAB}", True

SendKeys "YOUR_PASSWORD", True

Application.Wait 1000

SendKeys "~", True

End Sub

Replace the text "BROWSER_NAME" with the name of the browser (for example, "Firefox," "Internet Explorer," and "Google Chrome") you'll use to login to the web page. Replace the text "YOUR_LOGIN_ID," and "YOUR_PASSWORD" with the login information the web page requests.

Open the web page you'd like your code to log into.

Click one of the statements of the "sendPassword" subroutine, then press "F5" to run that subroutine. Windows will restore focus to the browser your program has activated. Your program will type and then send the login information you encoded in it.

Paste the following revision to the first program below that program in the VBA development window. This version of "sendPassword" will enter login information not from code, but from values on a worksheet in the current workbook. The main advantage of this approach is the convenience of changing the login information. Data stored in a worksheet is more convenient to change than it is to program code.

Public Sub sendPasswordStoredInWorksheet()

Dim login, pword, app

app = ActiveSheet.Cells(1, 1).Value

login = ActiveSheet.Cells(2, 1).Value

pword = ActiveSheet.Cells(3, 1).Value

AppActivate app, True

SendKeys login, True

Application.Wait 1000

SendKeys "{TAB}", True

SendKeys pword, True

Application.Wait 1000

SendKeys "~", True

End Sub

Type the browser name and login information you coded into the first version of the program in the top three cells of the Excel workbook's first worksheet.

Log out, in your browser, from the website your first program logged into, then return to the site's login page. Run the new version of the program, using instructions from step 6. The login will commence as it did in step 6.

Cite this Article A tool to create a citation to reference this article Cite this Article

About the Author

Darrin Koltow wrote about computer software until graphics programs reawakened his lifelong passion of becoming a master designer and draftsman. He has now committed to acquiring the training for a position designing characters, creatures and environments for video games, movies and other entertainment media.