Calling a function inside a sub routine

woodworkingpa

New Member
Joined
Oct 16, 2018
Messages
1
Hello friends,
After several searches on google, it looks that MrExcel is the best place to get VBA Excel help.
I am trying to login to different websites automatically. The goal is create a function that will handle all repetitive tasks inside the sub routine, so the code for each different website login will be shorter.

First here is the code that is currently working for one test website "FaceBook":

Sub OpenFacebook()Dim ie As Object
Set ie = CreateObject("InternetExplorer.Application")
ie.navigate Sheets("Sheet1").Cells(2, 5) ' Sheet1, Row 2, column 5 has the Link to FaceBook
ie.Visible = True
Do While ie.Busy And Not ie.readyState = 4
DoEvents
Loop
DoEvents
On Error Resume Next
ie.document.all.Item("email").Value = Sheets("Sheet1").Range("B2") ' Cell "B2" in Sheet1 has the user name
ie.document.all.Item("pass").Value = Sheets("Sheet1").Range("C2") ' Cell "C2" in Sheet1 has the password
ie.document.all.Item("loginbutton").Click​
End Sub

I tried to create a function/private sub called "OpenLink" that could handle the repetitive tasks as shown below, but it is not working:

Private Sub OpenLink(ByVal url As String, ByVal isVisible As Boolean)
Dim ie As Object
Set ie = CreateObject("InternetExplorer.Application")
ie.navigate url
ie.Visible = isVisible

Do While ie.Busy And Not ie.readyState = 4
DoEvents
Loop
DoEvents​
End Sub

And then I am including "OpenLink" in the sub routine:

Sub OpenFacebookTest()
OpenLink Sheets("Sheet1").Cells(2, 5), True
On Error Resume Next
ie.document.all.Item("email").Value = Sheets("Sheet1").Range("B2")
ie.document.all.Item("pass").Value = Sheets("Sheet1").Range("C2")
ie.document.all.Item("loginbutton").Click​
End Sub

When I step through it, it does not show me any errors, but it does not automatically enters the user name and password in the fields and the submit button is not clicked.

Any help you can provide will be greatly appreciate it.
Thank you
Toni Ln.
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"

Forum statistics

Threads
1,224,823
Messages
6,181,178
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top