Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Option Explicit
- 'VBE > Tools > References:'1: Microsoft HTML Object library 2: Microsoft Internet Controls
- Public Sub GetSoccerStats()
- Dim ie As Object, t As Date
- Dim objDoc As New MSHTML.HTMLDocument, text As String
- Dim lastRow As Long, dataSheet As Worksheet, inputArray(), i As Long
- Const MAX_WAIT_SEC As Long = 10
- Set dataSheet = ThisWorkbook.Worksheets("AVG GOAL DATA")
- Set ie = CreateObject("InternetExplorer.Application")
- With dataSheet
- lastRow = .Cells(.rows.Count, "B").End(xlUp).Row
- End With
- inputArray = dataSheet.Range("C4:E" & lastRow).Value
- inputArray = GetLinks(inputArray)
- Dim results(), r As Long, c As Long
- ReDim results(1 To UBound(inputArray, 1), 1 To 8)
- With ie
- .Visible = True
- For i = LBound(inputArray, 1) To UBound(inputArray, 1)
- r = r + 1
- .Navigate2 inputArray(i, 4)
- While .Busy Or .readyState < 4: DoEvents: Wend
- ' may need additional wait here
- Dim objTable As MSHTML.HTMLTable, objTableRow As MSHTML.HTMLTableRow
- If .document.querySelectorAll(".list-tabs--secondary").Length > 0 Then
- 'championship tab present
- 'switch to main
- .document.querySelector(".list-tabs--secondary a").Click
- While .Busy Or .readyState < 4: DoEvents: Wend
- Else 'you don't need this part
- 'Championship tab is not present
- End If
- t = Timer
- Do
- DoEvents
- On Error Resume Next
- Set objTable = .document.getElementsByClassName("table-main leaguestats")(0)
- On Error GoTo 0
- If Timer - t > MAX_WAIT_SEC Then Exit Do
- Loop While objTable Is Nothing
- If Not objTable Is Nothing Then
- c = 1
- For Each objTableRow In objTable.rows
- text = objTableRow.Cells(0).innerText
- Select Case text
- Case "Matches played", "Matches remaining", "Home goals", "Away goals"
- results(r, c) = objTableRow.Cells(1).innerText
- results(r, c + 1) = objTableRow.Cells(2).innerText
- c = c + 2
- End Select
- Next objTableRow
- End If
- Set objTable = Nothing
- Next
- .Quit
- End With
- dataSheet.Range("F4").Resize(UBound(results, 1), UBound(results, 2)) = results
- End Sub
- Public Function GetLinks(ByRef inputArray As Variant) As Variant
- Dim i As Long
- ReDim Preserve inputArray(1 To UBound(inputArray, 1), 1 To UBound(inputArray, 2) + 1)
- For i = LBound(inputArray, 1) To UBound(inputArray, 1)
- inputArray(i, 4) = IIf(inputArray(i, 1) = "CURRENT", inputArray(i, 2), inputArray(i, 3))
- Next
- GetLinks = inputArray
- End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement