简体   繁体   中英

I need help inputting a column from excel in VBA with html macro.

I selected 2 cells and named them "perch" and "stats". using what i inputted in the cell, i was able to run a search and retrieve the data i wanted from the html code(which i changed for safety). But my problem is want to input an entire column not just a single cell. And produce multiple results in a single column. Also the number of inputs will change periodically ~300, so i can't stipulate a specific range.

 Private Sub Worksheet_Change(ByVal Target As Range)
 If Target.Row = Range("perch").Row And _
 Target.Column = Range("perch").Column Then
     Dim IE As New InternetExplorer
     'IE.Visible = True
     IE.navigate "http://"google/not_real_link"
Do
 DoEvents
  Loop Until IE.readyState = READYSTATE_COMPLETE
  Dim Doc As HTMLDocument
  Set Doc = IE.document
  Dim sTD As String
  sTD = Trim(Doc.getElementsByTagName("td")(33).innerText)
  IE.Quit
  Range("stats").Value = sTD


 End If
 End Sub

You'll usually see the Worksheet_SelectionChange event used instaed of the Worksheet_Change .

Here are some tips on using these events:

  • Turn off EnableEvents. If you don't you might re-trigger the event and get stuck in an infinite loop

  • Use the Intersect method to test to see if your range and the Target intersect.

  • You should use Target.Cells.Count to see if multiple cells are being effected.

Private Sub Worksheet_SelectionChange(ByVal Target As Range) If Not Intersect(Target, Range("perch")) Is Nothing Then Application.EnableEvents = False

    Application.EnableEvents = True
End If

End Sub

I'm not really sure how you want to handle multiple values.

Creating a function to add value(s) to the end of a list often helps.

Sub AddStatusRow(value As Variant)
    Const StatColumn = 5
    Dim lastRow As Long

    lastRow = Range(Cells(1, StatColumn), Cells(Rows.count, StatColumn)).End(xlUp).Row + 1

    Cells(lastRow, StatColumn) = value

End Sub

You might want to consider iterating over the rows of the table instead of Doc.getElementsByTagName("td")(33).innerText .

You can find lots of great examples of what you want to do from the URL below.

http://www.tushar-mehta.com/publish_train/xl_vba_cases/vba_web_pages_services/index.htm

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