1

I want to get data from an Internet site.

I get

run-time error '91'

I modified earlier codes.

Sub DENEME()
    Dim S As String
    Dim html As HTMLDocument
    Dim hTable As HTMLTable
    Dim clipboard As Object
    Set html = New HTMLDocument
    With New XMLHTTP60
        .Open "GET", "https://www.scorespro.com/basketball/results/date/2019-02-15", False
        .setRequestHeader "content-type", "application/x-www-form-urlencoded; charset=UTF-8"
        .send
        S = .responseText
    End With
    html.body.innerHTML = S
    Set hTable = html.querySelector(".matches-data")
    Set clipboard = GetObject("New:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
    clipboard.SetText hTable.outerHTML
    clipboard.PutInClipboard
    Range("A1").PasteSpecial
End Sub
Community
  • 1
  • 1
Cumhur Ay
  • 83
  • 8

2 Answers2

1

Your selector string is set up so that it looks for the first element whose class is matches-data. However, matches-data is the id. As a result, hTable is being assigned Nothing, hence the error. Instead, try the following...

Set hTable = html.querySelector("#matches-data")
Domenic
  • 7,844
  • 2
  • 9
  • 17
1

A few points to note.

  1. Yes it is id not class for that element but that element is a div and copy pasting via clipboard the outer html will only paste the html of the div - not all tables within.
  2. What you want is a list of tables so you need to change your selector to get the tables within that div
  3. As using clipboard and there are merged output cells you need to find the last used row independent of column and add 1 to write out next table to a few row.
  4. If unsure about your selector use the search bar in browser as shown [here].1

VBA:

Option Explicit
Public Sub Deneme()
    Dim s As String, ws As Worksheet, tables As Object, i As Long
    Dim html As HTMLDocument, clipboard As Object
    Set html = New HTMLDocument
    Set ws = ThisWorkbook.Worksheets("Sheet1")
    Application.ScreenUpdating = False
    With New XMLHTTP60
        .Open "GET", "https://www.scorespro.com/basketball/results/date/2019-02-15", False
        .setRequestHeader "content-type", "application/x-www-form-urlencoded; charset=UTF-8"
        .send
        s = .responseText
    End With

    html.body.innerHTML = s
    Set tables = html.querySelectorAll("#matches-data table")
    Set clipboard = GetObject("New:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")

    For i = 0 To tables.Length - 1
        clipboard.SetText tables.item(i).outerHTML
        clipboard.PutInClipboard
        ws.Range("A" & GetLastRow(ws) + 1).PasteSpecial
    Next
    Application.ScreenUpdating = True
End Sub

Public Function GetLastRow(ByVal sh As Worksheet) As Long
    On Error Resume Next
    GetLastRow = sh.Cells.Find(What:="*", _
                            After:=sh.Range("A1"), _
                            Lookat:=xlPart, _
                            LookIn:=xlFormulas, _
                            SearchOrder:=xlByRows, _
                            SearchDirection:=xlPrevious, _
                            MatchCase:=False).Row
    On Error GoTo 0
End Function
QHarr
  • 83,427
  • 12
  • 54
  • 101
  • Dear @QHarr, Why has my comment been deleted? – Cumhur Ay Jun 03 '19 at 07:36
  • 1
    There are site moderators that remove comments deemed unnecessary so they may have removed. Also, comments can be removed by scripts that run as part of site content curation. – QHarr Jun 03 '19 at 07:38
  • you are a great king. King of Excel-Vba. Infinite thanks. – Cumhur Ay Jun 03 '19 at 07:39
  • I think you're really super in the VBA. And I want this to be known. – Cumhur Ay Jun 03 '19 at 07:41
  • 1
    That is kind. The way the site works is that appreciation for content is via voting. The idea is to curate quality content that serves future readers so anything other than that, however kind, can be removed. – QHarr Jun 03 '19 at 07:43
  • 1
    I understand. Thank you for answering all my questions and showing your interest. – Cumhur Ay Jun 03 '19 at 07:51