Hi I am trying use VBA to look property tax records off of. I’d like to return the owner of a property in column B, given the address is column A.
https://publictax.smith-county.com/Search/Results?
The code I’ve developed will go to the above URL and successfully:
- Change the drop down on the left to “Property Address”
- Click the search button on the right
My code is having difficulty returning to Column B the owner’s full name on the next page.
Query.SearchField=5&Query.SearchText=2115+Crestwood+Dr&Query.SearchAction=&Query.IncludeInactiveAccounts=False&Query.PayStatus=Both
Sub GetOwnerNames()
Dim IE As Object
Dim ws As Worksheet
Dim lastRow As Long
Dim address As String
Dim ownerName As String
Dim i As Long
Dim cardBodies As Object
Dim cardBody As Object
Dim smallElements As Object
Dim smallElement As Object
Dim locationFound As Boolean
Dim ownerElements As Object
Dim ownerElement As Object
Dim siblingElement As Object
' Create a new Internet Explorer instance
On Error Resume Next
Set IE = CreateObject("InternetExplorer.Application")
If IE Is Nothing Then
MsgBox "Could not create Internet Explorer instance", vbCritical
Exit Sub
End If
On Error GoTo 0
IE.Visible = True ' Set to True if you want to see the IE window
' Set the worksheet and find the last row with data in column A
Set ws = ThisWorkbook.Sheets("Sheet1") ' Change to your sheet name
lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
For i = 2 To lastRow ' Assuming the first row is a header
address = ws.Cells(i, 1).Value
If address <> "" Then
' Navigate to the search page
IE.Navigate "https://publictax.smith-county.com/search"
Do While IE.Busy Or IE.readyState <> 4: DoEvents: Loop
' Select "Property Address" from the dropdown
Set DropDown = IE.document.getElementById("Query.SearchField")
If DropDown Is Nothing Then
MsgBox "Could not find dropdown element", vbCritical
GoTo Cleanup
End If
DropDown.Value = 5 ' Value for "Property Address"
DropDown.FireEvent "onchange"
Do While IE.Busy Or IE.readyState <> 4: DoEvents: Loop
' Find the search box, input the address, and submit the search
Set searchBox = IE.document.getElementById("Query.SearchText")
If searchBox Is Nothing Then
MsgBox "Could not find search box element", vbCritical
GoTo Cleanup
End If
searchBox.Value = address
Do While IE.Busy Or IE.readyState <> 4: DoEvents: Loop
' Click the search button
Set searchButton = IE.document.querySelector("button.btn.btn-primary.col[type=submit]")
If searchButton Is Nothing Then
MsgBox "Could not find search button element", vbCritical
GoTo Cleanup
End If
searchButton.Click
Do While IE.Busy Or IE.readyState <> 4: DoEvents: Loop
' Wait for the results to load completely
Application.Wait Now + TimeValue("00:00:10")
' Extract elements to find "Property Location" and "Owner Name"
ownerName = "Not Found"
Set cardBodies = IE.document.querySelectorAll("div.card-body")
If cardBodies Is Nothing Then
MsgBox "Could not find card-body elements", vbCritical
GoTo Cleanup
End If
For Each cardBody In cardBodies
locationFound = False
Set smallElements = cardBody.getElementsByTagName("small")
For Each smallElement In smallElements
If InStr(Trim(smallElement.innerText), "Property Location") > 0 Then
Debug.Print "Property Location found: " & Trim(smallElement.innerText)
If Not smallElement.NextElementSibling Is Nothing Then
If InStr(Trim(smallElement.NextElementSibling.innerText), address) > 0 Then
Debug.Print "Address matched: " & Trim(smallElement.NextElementSibling.innerText)
locationFound = True
Exit For
End If
End If
End If
Next smallElement
' If the property location is found, get the owner name
If locationFound Then
Set ownerElements = cardBody.getElementsByTagName("strong")
For Each ownerElement In ownerElements
If Trim(ownerElement.innerText) <> "" And Trim(ownerElement.innerText) <> "Account Number" And _
Trim(ownerElement.innerText) <> "Total Due" And Trim(ownerElement.innerText) <> "Owner Name" And _
Trim(ownerElement.innerText) <> "Type" And Trim(ownerElement.innerText) <> "Property Location" Then
ownerName = ownerElement.innerText
Exit For
End If
Next ownerElement
Exit For
End If
Next cardBody
' Write the owner name back to the worksheet
ws.Cells(i, 2).Value = ownerName
End If
Next i
Cleanup:
‘ Clean up
On Error Resume Next
IE.Quit
Set IE = Nothing
On Error GoTo 0
MsgBox "Owner names have been updated.", vbInformation
End Sub
I’ve tried using AI to help correct the code, however The VBA either returns “not found”, or it will successfully, but incorrectly grab the account number that you see in the screenshot below. How can I adjust this VBA to grab the name “Walker Janis Kimberly” and past it in column B, before moving onto the next address?