Advertisement
qharr

Untitled

Apr 30th, 2019
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.00 KB | None | 0 0
  1. Option Explicit
  2.  
  3. 'VBE > Tools > References:'1: Microsoft HTML Object library 2: Microsoft Internet Controls
  4. Public Sub GetSoccerStats()
  5. Dim ie As Object, t As Date
  6. Dim objDoc As New MSHTML.HTMLDocument, text As String
  7. Dim lastRow As Long, dataSheet As Worksheet, inputArray(), i As Long
  8.  
  9.  
  10. Const MAX_WAIT_SEC As Long = 10
  11.  
  12. Set dataSheet = ThisWorkbook.Worksheets("AVG GOAL DATA")
  13. Set ie = CreateObject("InternetExplorer.Application")
  14. With dataSheet
  15. lastRow = .Cells(.rows.Count, "B").End(xlUp).Row
  16. End With
  17.  
  18.  
  19. inputArray = dataSheet.Range("C4:E" & lastRow).Value
  20. inputArray = GetLinks(inputArray)
  21.  
  22.  
  23. Dim results(), r As Long, c As Long
  24. ReDim results(1 To UBound(inputArray, 1), 1 To 8)
  25.  
  26.  
  27. With ie
  28. .Visible = True
  29. For i = LBound(inputArray, 1) To UBound(inputArray, 1)
  30. r = r + 1
  31. .Navigate2 inputArray(i, 4)
  32.  
  33.  
  34. While .Busy Or .readyState < 4: DoEvents: Wend
  35.  
  36. ' may need additional wait here
  37. Dim objTable As MSHTML.HTMLTable, objTableRow As MSHTML.HTMLTableRow
  38.  
  39. If .document.querySelectorAll(".list-tabs--secondary").Length > 0 Then
  40. 'championship tab present
  41. 'switch to main
  42. .document.querySelector(".list-tabs--secondary a").Click
  43.  
  44. While .Busy Or .readyState < 4: DoEvents: Wend
  45. Else 'you don't need this part
  46. 'Championship tab is not present
  47. End If
  48.  
  49. t = Timer
  50. Do
  51. DoEvents
  52. On Error Resume Next
  53. Set objTable = .document.getElementsByClassName("table-main leaguestats")(0)
  54. On Error GoTo 0
  55. If Timer - t > MAX_WAIT_SEC Then Exit Do
  56. Loop While objTable Is Nothing
  57.  
  58.  
  59. If Not objTable Is Nothing Then
  60. c = 1
  61. For Each objTableRow In objTable.rows
  62. text = objTableRow.Cells(0).innerText
  63. Select Case text
  64. Case "Matches played", "Matches remaining", "Home goals", "Away goals"
  65. results(r, c) = objTableRow.Cells(1).innerText
  66. results(r, c + 1) = objTableRow.Cells(2).innerText
  67. c = c + 2
  68. End Select
  69. Next objTableRow
  70. End If
  71. Set objTable = Nothing
  72. Next
  73. .Quit
  74. End With
  75. dataSheet.Range("F4").Resize(UBound(results, 1), UBound(results, 2)) = results
  76. End Sub
  77.  
  78. Public Function GetLinks(ByRef inputArray As Variant) As Variant
  79. Dim i As Long
  80. ReDim Preserve inputArray(1 To UBound(inputArray, 1), 1 To UBound(inputArray, 2) + 1)
  81.  
  82.  
  83. For i = LBound(inputArray, 1) To UBound(inputArray, 1)
  84. inputArray(i, 4) = IIf(inputArray(i, 1) = "CURRENT", inputArray(i, 2), inputArray(i, 3))
  85. Next
  86. GetLinks = inputArray
  87. End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement