Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

How do I write two codes in one module?

0

I have two codes for an automatic form fill. The first one is for the login and the second one for the form. If I only knew how to put them together instead of having two different buttons and two browser pages. Both codes work with no problem when put in different modules. I am new at this so if some could help me it would mean a lot.

[CODE]

CODE 1:

Dim HTMLDoc As HTMLDocument

Dim MyBrowser As InternetExplorer

Sub EW()

Dim MyHTML_Element As IHTMLElement

Dim MyURL As String

MyURL = "link"

Set MyBrowser = New InternetExplorer

MyBrowser.Silent = True

MyBrowser.navigate MyURL

MyBrowser.Visible = True

Do

Loop Until MyBrowser.readyState = READYSTATE_COMPLETE

Set HTMLDoc = MyBrowser.document

HTMLDoc.all.UserName.Value = "userdsfkjgh" 'Enter your email id here

HTMLDoc.all.Password.Value = "dstzdsrtz" 'Enter your password here

For Each MyHTML_Element In HTMLDoc.getElementsByTagName("input")

If MyHTML_Element.Type = "submit" Then MyHTML_Element.Click: Exit For

Next

End Sub

CODE 2:

Option Explicit

Sub EW()

Dim IE As InternetExplorer

Set IE = New InternetExplorer

IE.Visible = True

IE.navigate "sdfghdghf"

Do While IE.readyState <> READYSTATE_COMPLETE

DoEvents

Loop

Dim doc As HTMLDocument

Set doc = IE.document

doc.getElementById("_data").Value = "05.02.2021"

doc.getElementById("_data2").Value = "10.02.2021"

doc.getElementById("data3").Value = "dfg"

End Sub

Answer
Discuss

Answers

1

Your two procedures both have the same name. That, as you know can't fly. Therefore I renamed them as EW_1 and EW_2. Then, instead of calling them indicidually, you might create a third sub that calls them one after the other.

Sub CallBoth()
    ' 180

    EW_1
    EW_2
End Sub

Now, instead of calling the two subs you just call the master, either by button or any other way you prefer.

You say that your two procedures work individually. Therefore I haven't checked them but I did notice the following discrepancies that you may like to look into.

  1. The first procedure misses the DoEvents in the Do loop.
  2. The Long variable READYSTATE_COMPLETE isn't defined anywhere.
    (It might be in the DLL which I didn't load.)

And, of course, there is little point in letting each procedure create its own New InternetExplorer and extract the same document from it. So, you create both in the Master procedure and pass the document as an argument to both. That weould look like that:-

Option Explicit

Sub CallBoth()
    ' 180

    Dim MyURL As String
    Dim HTMLDoc     As HTMLDocument
    Dim MyBrowser   As InternetExplorer

    MyURL = "link"
    Set MyBrowser = New InternetExplorer
    With MyBrowser
        .Silent = True
        .navigate MyURL
        .Visible = True

        Do
            DoEvents
        Loop Until .readyState = READYSTATE_COMPLETE
        Set HTMLDoc = .document
    End With

    EW_1 HTMLDoc
    EW_2 HTMLDoc
End Sub

Sub EW_1(HTMLDoc As HTMLDocument)
    ' 180

    Dim MyHTML_Element As IHTMLElement

    With HTMLDoc.all
        .UserName.Value = "userdsfkjgh" 'Enter your email id here
        .Password.Value = "dstzdsrtz" 'Enter your password here
    End With
    For Each MyHTML_Element In HTMLDoc.getElementsByTagName("input")
        With MyHTML_Element
            If .Type = "submit" Then
                .Click
                Exit For
            End If
        End With
    Next
End Sub

Sub EW_2(Doc As HTMLDocument)
    ' 180

    With Doc
        .getElementById("_data").Value = "05.02.2021"
        .getElementById("_data2").Value = "10.02.2021"
        .getElementById("data3").Value = "dfg"
    End With
End Sub

Note that the name change of the HTMLDocument is a legacy of your original code, not reasonable but permissible. Unfortunately, I couldn't test this code but if you find flaws in it you should be easily able to fix them once you get the hang of what's going on. Good luck with that.

Discuss


Answer the Question

You must create an account to use the forum. Create an Account or Login