Select an item in an IE combobox - vba

I've got the correct code to navigate to a website where I want to start populating the dropdown lists and fields etc. The code I've been trying, following #Bond 's suggestion, is:
Sub GetQuote()
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
IE.navigate ("website")
IE.Visible = True
Do
DoEvents
Loop Until IE.readystate = 4
Dim e
Set e = IE.document.getElementsByClassname("id of button")(1)
e.Click
Application.Wait (Now + TimeValue("00:00:02"))
Do
DoEvents
Loop Until IE.readystate = 4
Dim z As Object
Set z = IE.document.getElementbyid("vehicleYearOfManufactureList")
z.SelectedIndex = 4
End Sub
This used to populate the first dropdown with the value 2012 but now I just get a run-time error where object is required. Could it be because previously the link that is clicked on used to open in the same and current window, and now it's somehow opening an entirely new window? I'm just baffled by how the same code works and then doesn't work, assuming no changes have been made to the source code it references.
The line that is highlighted in the debug of this error is: Set z = IE.document.getElementbyid("vehicleYearOfManufactureList"). As per the comment I made below, it's a "run-time 424: object" required error

You can do this a few ways. If you know the index of the item, you can set it directly using the <select> element's SelectedIndex property. In your case, "2012" is the 4th item in the dropdown:
Set e = IE.document.getElementbyid("vehicleYearOfManufactureList")
e.SelectedIndex = 4
Alternatively, you can iterate all of the <option> elements in the dropdown to find the value you're looking for, then set its Selected property to True:
Set e = IE.document.getElementbyid("vehicleYearOfManufactureList")
Dim o
For Each o In e.Options
If o.Value = "2012" Then
o.Selected = True
Exit For
End If
Next

Related

Excel VBA loop to input data in webpage

I have gone through many articles and able to input data in to webpage from excel. Now I am trying to use a loop because I have to entry data for 10 to 50 times in the same page.
Here is the code
Sub vfcp()
Set objIE = CreateObject("InternetExplorer.Application")
objIE.Top = 100
objIE.Left = 100
objIE.Width = 800
objIE.Height = 800
objIE.AddressBar = 1
objIE.StatusBar = 1
objIE.Toolbar = 1
objIE.Visible = True
Dim Input
Range("A2").Select
objIE.Navigate ("https://www.askebsa.dol.gov/VFCPCalculator/WebCalculator.aspx")
Do Until Selection.Offset(0, 0).Value = ""
If objIE.ReadyState = 4 Then
Input = Selection.Offset(0, 2).Value
objIE.document.GetElementByID("_ctl0_MainContent_txtPrincipal").Value = Input
objIE.document.GetElementByID("_ctl0_MainContent_cmdCalculate").Click
Selection.Offset(1, 0).Select
End If
Loop
End Sub
The problems in this code is, I have a list of 10 date. the loop inputs the last date of the list. I am trying to input the first date and click submit, then wait for the page to load and input the second date, and stop the loop if there is an empty cell.
Where is the code to enter the dates? You cannot click without them.
"then wait for the page to load and input the second date"
you can achieve this by inserting the following between your End If and Loop lines.
'Allows IE to load
While objIE.ReadyState <> 4
DoEvents
Wend
This shouldn't be too hard. I went to that URL, hit F12 to see the code behind the site, and started searching for the appropriate ID names ('Principal:', 'Loss Date:', etc). That's about it. Run the script to see a working version. Oh, yeah, set a reference to 'Microsoft Internet Controls' before you run the code.
Sub TryThis()
Dim ie As SHDocVw.InternetExplorer
Set ie = New SHDocVw.InternetExplorer
ie.Visible = True
ie.Navigate "https://www.askebsa.dol.gov/VFCPCalculator/WebCalculator.aspx"
Do
DoEvents
Loop Until ie.readystate = 4
'ie READYSTATE has 5 different status codes, here we are using status 4:
Call ie.Document.GetElementByID("_ctl0_MainContent_txtPrincipal").SetAttribute("value", "100000")
Call ie.Document.GetElementByID("_ctl0_MainContent_txtLossDateMonth").SetAttribute("value", "01")
Call ie.Document.GetElementByID("_ctl0_MainContent_txtLossDateDay").SetAttribute("value", "01")
Call ie.Document.GetElementByID("_ctl0_MainContent_txtLossDateYear").SetAttribute("value", "2016")
Call ie.Document.GetElementByID("_ctl0_MainContent_txtRecoveryDateMonth").SetAttribute("value", "01")
Call ie.Document.GetElementByID("_ctl0_MainContent_txtRecoveryDateDay").SetAttribute("value", "01")
Call ie.Document.GetElementByID("_ctl0_MainContent_txtRecoveryDateYear").SetAttribute("value", "2017")
Call ie.Document.GetElementByID("_ctl0_MainContent_txtFinalPaymentMonth").SetAttribute("value", "12")
Call ie.Document.GetElementByID("_ctl0_MainContent_txtFinalPaymentDay").SetAttribute("value", "30")
Call ie.Document.GetElementByID("_ctl0_MainContent_txtFinalPaymentYear").SetAttribute("value", "2017")
'to find the "<input……/>" tag hyper link and to click the button
Set AllInputs = ie.Document.getelementsbytagname("input") 'you can use any tagname as you wish
For Each hyper_link In AllInputs
If hyper_link.Name = "_ctl0:MainContent:cmdCalculate" Then 'you can use .name, .id etc
hyper_link.Click
Exit For
End If
Next
Do
DoEvents
Loop Until ie.readystate = 3
Do
DoEvents
Loop Until ie.readystate = 4
End Sub
If you want to scrape the results, refresh the URL and do the same as I described above: F12, search for the relevant IDs and/or names, and pull those into a Worksheet in Excel. I'll leave it to you to figure out that part. It will be a good learning experience!!!

VBA checkbox website interaction

I'm currently experiencing a problem with VBA. I'm trying to tick checkbox on an external website. Here is a html snippet that I'm working with:Html code from external website
I have redacted any confidential information from the snippet.
Here is my VBA code:
Set elements = objIE.document.getElementsByTagName("input")
For Each ele In elements
ele.toString
If ele.Value = "xxx" Then ele.Click
Next
So in this code the elementsvariable is an object and the loop variable ele is not being populated at all. I need to compare that the value in the check box contains value xxx. I'm not too experienced in VBA.
Any help would be appreciated
Thanks
I really don't know if that works. But in VBA the
.Value
property of a Checkbox is eighter true or false. The property of the text that is shown is:
.Caption
This code is looping properly:
Sub test()
Set objIE = CreateObject("InternetExplorer.Application")
Dim elements, ele
objIE.Visible = True
For Each ip In Sheets("Sheet1").Range("A2:A13").Value
objIE.Navigate ip
Do Until Not objIE.Busy And objIE.ReadyState = 4
DoEvents
Loop
Set elements = objIE.document.getElementsByTagName("input")
For Each ele In elements
ele.toString
If ele.Value = "xxx" Then ele.Click
Next
Next
End Sub

VBA Internet Explorer dropdown list trouble

I'm trying to scrape some data using VBA via Excel. I'm pretty rusty with VBA but I'm confident in my google-fu, 5+ hours in and no luck yet. I have seen many ways to work with drop-down menus but none seem to work with this one. I have successfully navigated to https://www.kbb.com/used-cars/ but I cannot work with the "year" drop-down. Maybe I am not using the correct id/name? Any advice is appreciated, let me know if I left any important info out.
Code:
Sub WebForumEntry()
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
IE.Top = 0
IE.Left = 0
IE.Width = 800
IE.Height = 600
IE.AddressBar = 0
IE.StatusBar = 0
IE.Toolbar = 0
IE.Navigate "http://www.kbb.com"
IE.Visible = True
Do
DoEvents
Loop Until IE.ReadyState = 4
'Click on the "Used Car Values" link
Set AllHyperlinks = IE.document.getelementsbytagname("A")
For Each hyper_link In AllHyperlinks
If hyper_link.innertext = "Used Car Prices" Then
hyper_link.Click
Exit For
End If
Next
'Select model year"
Set e = IE.document.getElementbyid("yearDropdown")
Dim o
For Each o In e.Options
If o.Value = "2012" Then
o.Selected = True
Exit For
End If
Next
End Sub
In the HTML there are no Option tags for you to select:
<div class="form-group">
<select id="yearDropdown" name="yearId" class="dropdown-one year-dropdown" data-default-option="Year" data-preselected-value=""></select>
</div>
The years are generated dynamically depending on whether you choose new/ used etc. So you can't select something that doesn't exist - perhaps you can directly set the value e.g.:
IE.document.getElementbyid("yearDropdown").value = "2012"
Edit
Note there is an issue in your original code:
'Click on the "Used Car Values" link
Set AllHyperlinks = IE.document.getelementsbytagname("A")
For Each hyper_link In AllHyperlinks
If hyper_link.innertext = "Used Car Prices" Then
hyper_link.Click
Exit For
End If
Next
'Select model year"
Set e = IE.document.getElementbyid("yearDropdown") '<-- error here if page not loaded per hyper_link.Click
When you call hyper_link.Click it will load another page (where the dropdowns are) and then per the Exit For the next command to run is Set e = IE... which will try and access an element that doesn't exist yet because the page is still loading per the hyper_link.Click. To work-around this you can just put in a pause like below:
Application.Wait Now + TimeValue("0:00:04")
It's a bit of a hack - and there may be better ways to solve it - but it will allow you to set the dropdown per my first suggestion :
Full code (re-write of yours):
Option Explicit
Sub WebForumEntry()
Dim IE As Object
Dim AllHyperLinks As Object
Dim hyper_link As Object
Dim e As Object
Set IE = CreateObject("InternetExplorer.Application")
IE.Top = 0
IE.Left = 0
IE.Width = 800
IE.Height = 600
IE.AddressBar = 0
IE.StatusBar = 0
IE.Toolbar = 0
IE.Navigate "http://www.kbb.com"
IE.Visible = True
' wait for page to load
Do
DoEvents
Loop Until IE.ReadyState = 4
'Click on the "Used Car Values" link
Set AllHyperLinks = IE.document.getelementsbytagname("A")
For Each hyper_link In AllHyperLinks
If hyper_link.innertext = "Used Car Prices" Then
hyper_link.Click
Exit For
End If
Next
'wait 4 seconds - so page fully loads
Application.Wait Now + TimeValue("0:00:04")
'Select model year"
Set e = IE.document.getElementbyid("yearDropdown")
' set a year
e.Value = "2016"
End Sub
...try and access an element that doesn't exist yet because the page is still loading...just put in a pause
Fantastic, thank you Robin!

Web scraping - create object for IE

Sub Get_Data()
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
ie.Navigate "http://www.scramble.nl/military-database/usaf"
Do While ie.Busy
Application.Wait DateAdd("s", 1, Now)
Loop
SendKeys "03-3114"
SendKeys "{ENTER}"
End Sub
The code below searches for keyboard typed value 03-3114 and gets a data in the table. If I 'd like to search for value which is already in cell A1 and scrape values from table for "Code, Type, CN, Unit" in cell range ("B1:E1") what should I do?
You are using SendKeys which are highly unreliable :) Why not find the name of the textbox and the search button and directly interact with it as shown below?
Sub Get_Data()
Dim ie As Object, objInputs As Object
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
ie.Navigate "http://www.scramble.nl/military-database/usaf"
Do While ie.readystate <> 4: DoEvents: Loop
'~~> Get the ID of the textbox where you want to output
ie.Document.getElementById("serial").Value = "03-3114"
'~~> Here we try to identify the search button and click it
Set objInputs = ie.Document.getElementsByTagName("input")
For Each ele In objInputs
If ele.Name Like "sbm" Then
ele.Click
Exit For
End If
Next
End Sub
Note: To understand how I got the names serial and sbm, refer to the explanation given just above the image below.
The code below searches for keyboard typed value 03-3114 and gets a data in the table. If I 'd like to search for value which is already in cell A1 and scrape values from table for "Code, Type, CN, Unit" in cell range ("B1:E1") what should I do?
Directly put the value from A1 in lieu of the hardcoded value
ie.Document.getElementById("serial").Value = Sheets("Sheet1").Range("A1").Value
To get the values from the table, identify the elements of the table by right clicking on it in the browser and clicking on "Inspect/Inspect Element(In Chrome it is just Inspect)" as shown below.
I can give you the code but I want you to do it yourself. If you are still stuck then update the question with the code that you tried and then we will take it from there.
Interesting read: html parsing of cricinfo scorecards

Run-time error '91' (Object variable or With block variable not set)

I am relatively new to VBA and am trying to put together a msgbox that will give me a specific number from a web scrape, however I keep running into a run-time error '91' and I simply cannot figure out how to fix this. I have searched countless stackoverflow questions, youtube videos and generic google searches, however have not been successful in finding out the error on my own.
Here is the code:
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.Navigate ("http://brokercheck.finra.org")
Do
DoEvents
Loop Until IE.ReadyState = 4
'Enter values from the corresponding sheet
'Set some generic typing for ease
Set doc = IE.document
doc.GetElementbyID("GenericSearch_IndividualSearchText").Value = Worksheets("Master").Range("D203")
doc.GetElementbyID("GenericSearch_EmploymingFirmSearchText").Value = Worksheets("Master").Range("C203")
Set elements = doc.getElementsByTagName("button")
For Each element In elements
If element.getAttribute("type") = "submit" Then
element.Click
Exit For
End If
Next element
Do
DoEvents
Loop Until IE.ReadyState = 4
'find CRD#
Set crd = doc.getElementsByClassName("summarydisplaycrd")(0).innerText 'here is where the run time error occurs
MsgBox crd
and the HTML I am trying to get the information from:
<div class="searchresulttext">
<div class="bcrow">
<div class=""> <span class="summarydisplayname">[redacted]</span> <span class="summarydisplaycrd text-nowrap">(CRD# 5944070)</span></div>
I'm reviewing this code and the finra.org site, and have the following observations, which when addressed, should resolve the problem.
The HTML example you provided is simply incorrect, based on the actual HTML that is returned from the "Check" button.
The actual HTML returned looks like this, and the classname is "displayname", not "summarydisplaycrd":
<div class="SearchResultItemColor bcrow">
<div class="searchresulttext">
<div class="bcsearchresultfirstcol">
<span class="displayname">[redacted]</span> <span class="displaycrd">(CRD# 123456789)</span>
Your code exits the For each element loop upon finding the first "submit" button. This may not be the "Check" button (although I can get results either way, you may want to add more logic in the code to ensure the "Check " button is submit.
UPDATE
On further review, while I can replicate the Type 91 error, I still don't know why your class name appears different than mine (maybe an IE11 thing, dunno...) in any case, I'm able to resolve that by forcing a longer delay, as in this case the DoEvents loop is simply not adequate (sometimes this is the case when data is served dynamically from external functions, the browser is ReadyState=4 and .Busy=True, so the loop doesn't do anything)
I use the WinAPI Sleep function and force a 1 second delay after the "Click" button pressed, looping on condition of ReadyState = 4 and .Busy=True.
NOTE you will need to modify the classname parameter depending on how it is appearing on your HTML.
Option Explicit
Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Sub finra()
Dim IE As Object
Dim doc As Object, element As Object, elements As Object, crd
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.Navigate ("http://brokercheck.finra.org")
Call WaitIE(IE, 1000)
'Enter values from the corresponding sheet
'Set some generic typing for ease
Set doc = IE.document
doc.GetElementbyID("GenericSearch_IndividualSearchText").Value = "steve"
doc.GetElementbyID("GenericSearch_EmploymingFirmSearchText").Value = "ed"
Set elements = doc.getElementsByTagName("button")
For Each element In elements
If element.getAttribute("type") = "submit" Then
If element.innerText = "Check " Then
element.Click
Exit For
End If
End If
Next element
Call WaitIE(IE, 1000)
Dim itms As Object
'Set itms = doc.getElementsByClassName("displaycrd")
crd = doc.getElementsByClassName("displaycrd")(0).innerText 'here is where the run time error occurs
MsgBox crd
End Sub
Sub WaitIE(IE As Object, Optional time As Long = 250)
Dim i As Long
Do
Sleep time
Debug.Print CStr(i) & vbTab & "Ready: " & CStr(IE.ReadyState = 4) & _
vbCrLf & vbTab & "Busy: " & CStr(IE.Busy)
i = i + 1
Loop Until IE.ReadyState = 4 And Not IE.Busy
End Sub