I am getting this above-mentioned error on running. The purpose of this code is to take values from excel sheet and put two values from sheet into website then extract results from web in a sheet.
here is the code.
it shows error on this line Do While ie.Busy Or ie.readyState <> 4
i also used READYSTATE_COMPLETE it did not work too.
Sub WebAutomation()
Dim ie As Object
Dim html As Object
Dim inputFilePath As String
Dim wb As Workbook
Dim ws As Worksheet
Dim resultSheet As Worksheet
Dim lastRow As Long
Dim number1 As String
Dim number2 As String
Dim additionResult As String
Dim subtractionResult As String
Dim multiplicationResult As String
Dim divisionResult As String
Dim i As Long
Dim startTime As Single
' Define the input file path and open the workbook
inputFilePath = "C:Usersss483532OneDrive - GSKDocumentsCalculator.xlsx" ' Change this path to your actual file path
Set wb = Workbooks.Open(inputFilePath)
Set ws = wb.Sheets("Sheet1")
' Create a new sheet for results
On Error Resume Next
Application.DisplayAlerts = False
wb.Sheets("Results").Delete
Application.DisplayAlerts = True
On Error GoTo 0
Set resultSheet = wb.Sheets.Add
resultSheet.Name = "Results"
resultSheet.Range("A1:F1").Value = Array("Number 1", "Number 2", "Addition", "Subtraction", "Multiplication", "Division")
' Initialize IE
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
ie.FullScreen = True ' Maximize the browser window
' Get the last row of data
lastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
' Loop through each row of the input data
For i = 2 To lastRow ' Assuming the first row is headers
number1 = ws.Cells(i, 1).Value
number2 = ws.Cells(i, 2).Value
' Navigate to the web page
ie.Navigate "https://www.automationandagile.com/p/demo-page.html"
' Wait for the page to load
Do While ie.Busy Or ie.readyState <> 4
DoEvents
Loop
' Wait for the input element to be available
startTime = Timer
Do While Timer - startTime < 10
On Error Resume Next
Set html = ie.document
If Not html Is Nothing Then
If Not html.getElementById("num1") Is Nothing Then Exit Do
End If
On Error GoTo 0
DoEvents
Loop
If html Is Nothing Or html.getElementById("num1") Is Nothing Then
MsgBox "Failed to load the webpage or find the input elements.", vbCritical
Exit Sub
End If
' Fill the form and submit
html.getElementById("num1").Value = number1
html.getElementById("num2").Value = number2
html.getElementById("btnCalculate").Click
Application.Wait Now + TimeValue("00:00:02") ' Wait for results to load
' Retrieve results
On Error Resume Next
additionResult = html.getElementById("lblAdd").innerText
subtractionResult = html.getElementById("lblSub").innerText
multiplicationResult = html.getElementById("lblMult").innerText
divisionResult = html.getElementById("lblDiv").innerText
On Error GoTo 0
' Store the results in the results sheet
resultSheet.Cells(i, 1).Value = number1
resultSheet.Cells(i, 2).Value = number2
resultSheet.Cells(i, 3).Value = additionResult
resultSheet.Cells(i, 4).Value = subtractionResult
resultSheet.Cells(i, 5).Value = multiplicationResult
resultSheet.Cells(i, 6).Value = divisionResult
Next i
' Close IE
ie.Quit
' Save the workbook with results
wb.SaveAs Filename:=Left(inputFilePath, InStrRev(inputFilePath, ".") - 1) & "_results.xlsx", FileFormat:=xlOpenXMLWorkbook
wb.Close
MsgBox "Data extraction and calculation completed!", vbInformation
End Sub
New contributor
Saif Saeed is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.