简体   繁体   中英

Can't assign text to cell with Excel VBA

I'm trying to scrape zip codes from Google. I've been trying to put innertext into a cell, but I think I may be getting a variable mismatch on 2nd to last line.

'This Must go at the top of your module. It's used to set IE as the active window

Sub Automate_IE_Enter_Data()
'This will load a webpage in IE
    Dim i As Long
    Dim URL As String
    Dim IE As Object
    Dim objElement As Object
    Dim objCollection As Object
    Dim HWNDSrc As Long
    Dim adds As Variant, add As Variant
    Dim addt As String


    'Create InternetExplorer Object
    Set IE = CreateObject("InternetExplorer.Application")

    'Set IE.Visible = True to make IE visible, or False for IE to run in the background
    IE.Visible = True

    'Define URL
    URL = "https://www.google.com/search?ei=djKhW7nELYqs8AO96baoAw&q=1000 Westover Rd kansas city, Mo"

    'Navigate to URL
    IE.Navigate URL

    ' Statusbar let's user know website is loading
    Application.StatusBar = URL & " is loading. Please wait..."

    ' Wait while IE loading...
    'IE ReadyState = 4 signifies the webpage has loaded (the first loop is set to avoid inadvertantly skipping over the second loop)
    Do While IE.ReadyState = 4: DoEvents: Loop
    Do Until IE.ReadyState = 4: DoEvents: Loop

    'Webpage Loaded
    Application.StatusBar = URL & " Loaded"

    'Get Window ID for IE so we can set it as activate window
    HWNDSrc = IE.Hwnd
    'Set IE as Active Window
    'SetForegroundWindow HWNDSrc

    Debug.Print "ihgc"

    'Unload IE
endmacro:
    Set adds = IE.Document.getElementsbyClassName("desktop-title-subcontent")

        For Each add In adds

            Debug.Print add.innertext
        Next

        Cells(2, f).Value = add.innertext
End Sub

Couple of things. First and foremost, your loop is unnecessary. I ran your code, and there's nothing to loop. Even if it was necessary, it's being used improperly.

So, in assuming that you in fact do not need a For...Next loop, then you can use the index number of 0 for your collection of IE.Document.getElementsbyClassName("desktop-title-subcontent") , then set your cell reference equal to the innerText property of that collection item.

This brings me to the next issue, your cell reference. Cells(2, f) , the f is not a declared variable. If you where actually wanting to use the column "F", then you need to enclose 'F' in double quotes:
Cells(2, "F") or use the column's index of 6 , Cells(2, 6)

So, replace this entire portion:

Set adds = IE.Document.getElementsbyClassName("desktop-title-subcontent")

    For Each add In adds

        Debug.Print add.innertext
    Next

    Cells(2, f).Value = add.innertext

with this:

Cells(2, "F").Value = IE.Document.getElementsByClassName _
            ("desktop-title-subcontent")(0).innerText

OPTIONAL

And lastly, I would look into using Early Binding over late binding . It has many advantages, with a possible notable speed improvement.

You would need to set a reference to Microsoft Internet Controls and declare IE as type InternetExplorer vs Object . But that's not going to make or break your code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM