Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Option Explicit
- 'VBE > Tools > References:
- ' Microsoft Internet Controls
- Public Sub CheckTax()
- Dim ie As InternetExplorer, searchBox As Object, t As Date, ws As Worksheet
- Const MAX_WAIT_SEC As Long = 20
- Dim inputValues As Range, rng As Range, i As Long
- Set ie = New InternetExplorer
- Set ws = ThisWorkbook.Worksheets("INPUT & DATA RESULTS")
- Dim lastRow As Long
- With ws
- 'lastRow = .Cells(.rows.Count, "X").End(xlUp).Row '' If coming from bottom of page. Accounts for empty cells in range
- lastRow = .Cells(.rows.Count, "X").End(xlDown).Row ''Stops at first blank cell in range
- End With
- Set inputValues = ws.Range("X3:X" & lastRow).Value
- With ie
- .Visible = True
- For Each rng in inputValues
- .Navigate2 "https://vehicleenquiry.service.gov.uk/"
- While .Busy Or .readyState < 4: DoEvents: Wend
- t = Timer
- Do
- On Error Resume Next
- Set searchBox = .document.querySelector("#Vrm")
- On Error GoTo 0
- If Timer - t > MAX_WAIT_SEC Then Exit Do
- Loop While searchBox Is Nothing
- If searchBox Is Nothing Then
- Exit Sub
- Else
- searchBox.Focus
- searchBox.Value = rng.Value
- End If
- .document.querySelector(".button").Click
- While .Busy Or .readyState < 4: DoEvents: Wend
- If .document.querySelectorAll("h3").Length > 0 Then
- 'ws.Cells(rng.Row, "Y") = "Vehicle details could not be found"
- 'ws.Cells(rng.Row, "Z") = "Vehicle details could not be found"
- Else
- t = Timer
- Do
- If Timer - t > MAX_WAIT_SEC Then Exit Do
- Loop While ie.document.querySelectorAll("#Correct_True").Length = 0
- ie.document.querySelector("#Correct_True").Click
- While .Busy Or .readyState < 4: DoEvents: Wend
- .document.querySelector(".button").Click
- While .Busy Or .readyState < 4: DoEvents: Wend
- Dim items As Object, taxInfo As String, motInfo As String
- t = Timer
- Do
- On Error Resume Next
- Set items = ie.document.querySelectorAll("strong")
- On Error GoTo 0
- If Timer - t > MAX_WAIT_SEC Then Exit Do
- Loop While items.Length = 0
- taxInfo = Replace$(items.item(0).innerText, "Tax due: ", vbNullString)
- motInfo = Replace$(items.item(1).innerText, "Expires: ", vbNullString)
- ws.Cells(rng.Row, "Y") = taxInfo
- ws.Cells(rng.Row, "Z") = motInfo
- End If
- Set searchBox = Nothing: Set items = Nothing
- Next
- .Quit
- End With
- End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement