I am trying to prefill data from Excel to a local intranet website via VBA.
I've been able to prefill most data to the site, but I am struggling with this combodate box:
Please see HTML code:
<div class="form-group row " id="starttime_field">
<label class="col-form-label col-md-3" for="starttime">Start time</label>
<div class="col-md-9">
<input type="text" id="starttime" name="starttime" class="form-control" size="30" style="display: none;"><span class="combodate">
<select class="hour " style="width: auto;"><option value="0">00</option><option value="1">01</option><option value="2">02</option><option value="3">03</option><option value="4">04</option><option value="5">05</option><option value="6">06</option><option value="7">07</option><option value="8">08</option><option value="9">09</option><option value="10">10</option><option value="11">11</option><option value="12">12</option><option value="13">13</option><option value="14">14</option><option value="15">15</option><option value="16">16</option><option value="17">17</option><option value="18">18</option><option value="19">19</option><option value="20">20</option><option value="21">21</option><option value="22">22</option><option value="23">23</option></select> :
<select class="minute " style="width: auto;"><option value="0">00</option><option value="1">01</option><option value="2">02</option><option value="3">03</option><option value="4">04</option><option value="5">05</option><option value="6">06</option><option value="7">07</option><option value="8">08</option><option value="9">09</option><option value="10">10</option><option value="11">11</option><option value="12">12</option><option value="13">13</option><option value="14">14</option><option value="15">15</option><option value="16">16</option><option value="17">17</option><option value="18">18</option><option value="19">19</option><option value="20">20</option><option value="21">21</option><option value="22">22</option><option value="23">23</option><option value="24">24</option><option value="25">25</option><option value="26">26</option><option value="27">27</option><option value="28">28</option><option value="29">29</option><option value="30">30</option><option value="31">31</option><option value="32">32</option><option value="33">33</option><option value="34">34</option><option value="35">35</option><option value="36">36</option><option value="37">37</option><option value="38">38</option><option value="39">39</option><option value="40">40</option><option value="41">41</option><option value="42">42</option><option value="43">43</option><option value="44">44</option><option value="45">45</option><option value="46">46</option><option value="47">47</option><option value="48">48</option><option value="49">49</option><option value="50">50</option><option value="51">51</option><option value="52">52</option><option value="53">53</option><option value="54">54</option><option value="55">55</option><option value="56">56</option><option value="57">57</option><option value="58">58</option><option value="59">59</option></select></span><span class="combodate"><select class="hour " style="width: auto;"><option value="0">00</option><option value="1">01</option><option value="2">02</option><option value="3">03</option><option value="4">04</option><option value="5">05</option><option value="6">06</option><option value="7">07</option><option value="8">08</option><option value="9">09</option><option value="10">10</option><option value="11">11</option><option value="12">12</option><option value="13">13</option><option value="14">14</option><option value="15">15</option><option value="16">16</option><option value="17">17</option><option value="18">18</option><option value="19">19</option><option value="20">20</option><option value="21">21</option><option value="22">22</option><option value="23">23</option></select> : <select class="minute " style="width: auto;"><option value="0">00</option><option value="1">01</option><option value="2">02</option><option value="3">03</option><option value="4">04</option><option value="5">05</option><option value="6">06</option><option value="7">07</option><option value="8">08</option><option value="9">09</option><option value="10">10</option><option value="11">11</option><option value="12">12</option><option value="13">13</option><option value="14">14</option><option value="15">15</option><option value="16">16</option><option value="17">17</option><option value="18">18</option><option value="19">19</option><option value="20">20</option><option value="21">21</option><option value="22">22</option><option value="23">23</option><option value="24">24</option><option value="25">25</option><option value="26">26</option><option value="27">27</option><option value="28">28</option><option value="29">29</option><option value="30">30</option><option value="31">31</option><option value="32">32</option><option value="33">33</option><option value="34">34</option><option value="35">35</option><option value="36">36</option><option value="37">37</option><option value="38">38</option><option value="39">39</option><option value="40">40</option><option value="41">41</option><option value="42">42</option><option value="43">43</option><option value="44">44</option><option value="45">45</option><option value="46">46</option><option value="47">47</option><option value="48">48</option><option value="49">49</option><option value="50">50</option><option value="51">51</option><option value="52">52</option><option value="53">53</option><option value="54">54</option><option value="55">55</option><option value="56">56</option><option value="57">57</option><option value="58">58</option><option value="59">59</option></select></span>
This is what this section of the form looks like (highlighted in red):
ComboBox Form
Here is a snippet of the code I am using:
Sub Legends()
Dim IE As InternetExplorerMedium
Dim Environment As String
Dim webContent As String
Dim NOME_EMPRESA, CNPJ, CPF, COD_ACESSO As String
Dim Lookup_Range As Range
Dim SH
Dim confirmationBox As Integer
confirmationBox = MsgBox("Confirm Legendary Promotion changes to " & Worksheets("Legends").Range("E2") & "?", vbYesNoCancel, "Woolly Legends")
'Confirmation box data if yes is clicked
Select Case confirmationBox
Case vbYes
Dim eachIE
'Choosing the environment
If ThisWorkbook.Sheets("Legends").Range("E2").Value = ThisWorkbook.Sheets("Parameters").Range("A2") Then
AD = ThisWorkbook.Sheets("Parameters").Range("C2").Value
Environment = AD
End If
If ThisWorkbook.Sheets("Legends").Range("E2").Value = ThisWorkbook.Sheets("Parameters").Range("A3") Then
AC= ThisWorkbook.Sheets("Parameters").Range("C3").Value
Environment = AC
End If
If ThisWorkbook.Sheets("Legends").Range("E2").Value = ThisWorkbook.Sheets("Parameters").Range("A4") Then
AB = ThisWorkbook.Sheets("Parameters").Range("C4").Value
Environment = AB
End If
If ThisWorkbook.Sheets("Legends").Range("E2").Value = ThisWorkbook.Sheets("Parameters").Range("A5") Then
Testing = ThisWorkbook.Sheets("Parameters").Range("C5").Value
Environment = Testing
End If
Set IE = New SHDocVw.InternetExplorer
IE.navigate Environment
ShowWindow IE.hwnd, SW_MAXIMIZE
Do
Set SH = New Shell32.Shell
For Each eachIE In SH.Windows
If InStr(1, eachIE.LocationURL, Environment) Then
Set IE = eachIE
'ie.Visible = False 'This is here because in some environments, the new process defaults to Visible.
Exit Do
End If
Next eachIE
Loop
Set eachIE = Nothing
Set SH = Nothing
'Setting doc to allow for form manipulation via Excel
Set doc = IE.document
'Setting the Promotion Names for the Text Promotion
IE.document.all("promo_name").Value = ThisWorkbook.Sheets("Legends").Range("b3")
IE.document.all("race_name").Value = ThisWorkbook.Sheets("Legends").Range("b19")
'Setting start date and end date
IE.document.all("startdate").Value = Format$(ThisWorkbook.Sheets("Legends").Range("b7").Value, "yyyy-mm-dd")
IE.document.all("enddate").Value = Format$(ThisWorkbook.Sheets("Legends").Range("b8").Value, "yyyy-mm-dd")
'Setting the Runner Names
IE.document.all("runner_names_0").Value = ThisWorkbook.Sheets("Legends").Range("b20")
IE.document.all("runner_names_1").Value = ThisWorkbook.Sheets("Legends").Range("b21")
IE.document.all("runner_names_2").Value = ThisWorkbook.Sheets("Legends").Range("b22")
IE.document.all("runner_names_3").Value = ThisWorkbook.Sheets("Legends").Range("b23")
IE.document.all("runner_names_4").Value = ThisWorkbook.Sheets("Legends").Range("b24")
IE.document.all("runner_names_5").Value = ThisWorkbook.Sheets("Legends").Range("b25")
'Setting the dropdown box for the jurisdictions
If ThisWorkbook.Sheets("Legends").Range("B4").Value = "AAA" Or ThisWorkbook.Sheets("Legends").Range("B5").Value = "AAB" Or ThisWorkbook.Sheets("Legends").Range("B6").Value = "AAC" Then
IE.document.all("jurisdiction[]").Options(0).Selected = True
End If
If ThisWorkbook.Sheets("Legends").Range("B4").Value = "BBB" Or ThisWorkbook.Sheets("Legends").Range("B5").Value = "BBA" Or ThisWorkbook.Sheets("Legends").Range("B6").Value = "BBC" Then
IE.document.all("jurisdiction[]").Options(1).Selected = True
End If
If ThisWorkbook.Sheets("Legends").Range("B4").Value = "CCA" Or ThisWorkbook.Sheets("Legends").Range("B5").Value = "CCB" Or ThisWorkbook.Sheets("Legends").Range("B6").Value = "CCC" Then
IE.document.all("jurisdiction[]").Options(2).Selected = True
End If
End Select
End Sub
I am unsure how to pre-populate data from the spreadsheet into the hour and minute comboboxes, as the layout of this is a bit different from the other fields in the html.
Any help or guidance would be greatly appreciated.
Thanks!
The hour and minute boxes are select elements which means with InternetExplorer automation you have two main methods for selecting individual items (and a couple of alternates for tricky situations):
You can use selectedIndex on the select itself then specify index of option of interest in the list under that select. I think starts at 1 but you would need to check.
Use .Selected = True on the option element itself.
As you have options that have the same values you need to isolate the dropdowns by the parent select if using second method. This can be done by adding parent className in front.
Method 1:
hour
ie.document.querySelector(".hour").SelectedIndex = 1
minute
ie.document.querySelector(".minute").SelectedIndex = 1
Method 2:
hour
ie.document.querySelector(".hour [value='0']").Selected = True 'hour 00
minute
ie.document.querySelector(".minute [value='0']").Selected = True 'minute 00
Alternates are predominately the above/variants thereof written in javascript and executed by method of
ie.document.parentWindow.execScript. This can be helpful when elements are not interactable for some reason but generally frowned
upon by QA automation specialists in particular.
Reading:
CSS Selectors
EDIT:
To differentiate starttime from endtime based on html provided e.g.
ie.document.querySelector(".combodate + .combodate .hour [value='0']").Selected =True 'end hour
ie.document.querySelector(".combodate + .combodate .minute [value='0']").Selected =True 'end minute
Using variable
Dim var As Long
var = 0
ie.document.querySelector(".combodate .hour [value='" & cstr(var) & "']").Selected =True 'start hour
Although your HTML doesn't reflect this I would have expected something more like:
ie.document.querySelector("#starttime_field .combodate .hour [value='" & cstr(var) & "']").Selected =True
ie.document.querySelector("#endtime_field .combodate .hour [value='" & cstr(var) & "']").Selected =True
Related
I previously used the following code snippet to click on a selector button in a web page that was opened in internet explorer.
Set inputSelector = HTMLDoc.getElementsByClassName("idp") '<== the collection of input tags on the page
For Each MyHTML_Element In inputSelector
If MyHTML_Element.className = "idp" Then
MyHTML_Element.Click: Exit For
End If
Next
I tried to find the same button in the "idp" class with Selenium using
Set inputBoxes = htmlDoc.FindElementByCss("idp").Text
which is from an example I found on-line. This snippet generates a compiler error because .Text is a string and I'm seeking a collection. What should I use in place of FindElementByCss().Text to return a collection?
Public Sub Sample()
Dim pid As Long
Dim pno As Long: pno = 17556
Dim htmlDoc As WebDriver
'Create Selenium htmlDoc, Verify MS Edge Webdriver and continue
Set htmlDoc = CreateObject("Selenium.WebDriver")
pid = StartEdgeDriver(PortNo:=pno)
If pid = 0 Then Exit Sub
'Specify URL, create Selenium html document and open webpage
sLoginURL = "https://empowercentral.ussco.com/"
htmlDoc.StartRemotely "http://localhost:" & pno & "/", "MicrosoftEdge"
htmlDoc.Get sLoginURL
Set inputSelector = htmlDoc.FindElementByCss("idp").Text
For Each MyHTML_Element In inputSelector
If MyHTML_Element.className = "idp" Then
MyHTML_Element.Click: Exit For
End If
Next
htmlDoc.FindElementById("userNameInput").SendKeys "username"
htmlDoc.FindElementById("nextButton").Click
htmlDoc.FindElementById("passwordInput").SendKeys "password"
htmlDoc.FindElementById("submitButton").Click
End Sub
Here is the html from the website that I'm trying to scrape:
Sign in with one of these accounts
<input id="hrdSelection" type="hidden">
<div class="idp" tabindex="1" onkeypress="if (event && (event.keyCode == 32 || event.keyCode == 13)) HRD.selection('http://login.essendant.com/adfs/services/trust');" onclick="HRD.selection('http://login.essendant.com/adfs/services/trust'); return false;"><img class="largeIcon float" src="/adfs/portal/images/idp/idp.png?id=3EADD3E829A20DF612C7A77960FF811E66E3EE6BAE2C33C9B20E7478BAC87548" alt="Customer or Supplier"><div class="idpDescription float"><span class="largeTextNoWrap indentNonCollapsible">Customer or Supplier</span></div></div><div class="idp" tabindex="2" onkeypress="if (event && (event.keyCode == 32 || event.keyCode == 13)) HRD.selection('AD AUTHORITY');" onclick="HRD.selection('AD AUTHORITY'); return false;"><img class="largeIcon float" src="/adfs/portal/images/idp/localsts.png?id=77809433F1EA22C491CA1C7A0836B144BB32E92C12110BF2A2D30810B030734D" alt="Active Directory"><div class="idpDescription float"><span class="largeTextNoWrap indentNonCollapsible">Essendant Associate</span></div></div>
</div>
I have through an API fetched my data as an XML, and I wish to cycle through nodes (there are several of the same type) and add them to certain fields/a table.
Example from the XML-file:
<HistRating
xmlns="">
<EndrAr>2020</EndrAr>
<EndrMnd>7</EndrMnd>
<Rating>A</Rating>
</HistRating>
<HistRating
xmlns="">
<EndrAr>2019</EndrAr>
<EndrMnd>6</EndrMnd>
<Rating>A</Rating>
</HistRating>
I have tried the following format (at this point the XML I need is in a string in xmlDoc xmlDoc = CreateObject("MSXML2.DOMDocument.6.0"). Fully aware that this is not a really "sexy" way to write it, but I'm new at this game:
Set nodeXML = xmlDoc.getElementsByTagName("EndrAr")
Range("G1").Value = nodeXML(1).Text
Range("H1").Value = nodeXML(2).Text
Range("I1").Value = nodeXML(3).Text
Set nodeXML = xmlDoc.getElementsByTagName("EndrMnd")
Range("G2").Value = nodeXML(1).Text
Range("H2").Value = nodeXML(2).Text
Range("I2").Value = nodeXML(3).Text
Set nodeXML = xmlDoc.getElementsByTagName("Rating")
Range("G3").Value = nodeXML(1).Text
Range("H3").Value = nodeXML(2).Text
Range("I3").Value = nodeXML(3).Text
This works great as long as all three items are there. Unfortunately that is not given. If it is a new company i.e. (3) wont exist (there is one line per year above), and I would like to either set the cell to Blank or No value or something.
The result from when I run the above code:
But if I try to add a line 4 to test what happens if value does not exists I get the following (for obvious reasons)
What I would love some help with is:
Can I by some "magic" add a ifmissing (tried it, but could not get it to work)?
Other ways to add a if variable is not found, input following into cell
Or are there a complete different way I should have solved this?
This is to add accounting data from last X available years (where X is ie 4, or less if not 4 is available) from 30 nodes.
You could use an Error trapping Function. Note in the code below we choose not to use the returned boolean.
Dim myTest as String
.
.
TryReadingXmlNode nodeXML,1, myText
Range("G1").Value = myText
.
.
Public Function TryReadingXmlNode(ByVal ipNode as object, ByVal ipIndex as Long, ByRef opText as string) as boolean
On Error Resume Next
opText=ipNode.Item(ipIndex).Text
TryReadingXmlNode=Len(opText)>0
If err.number>0 then opText="NoValue"
on Error Goto 0
End Function
Start by querying all of the HistRating elements, then loop over that collection:
Const MAX_YEARS As Long = 4
Dim ratings, rating, c As Range, i as Long
Set c= Range("A1")
Set ratings = xmlDoc.getElementsByTagName("HistRating")
For Each rating in ratings
c.offset(0, i) = rating.getElementsByTagName("EndrAr")(0).Text
c.offset(1, i) = rating.getElementsByTagName("EndrMnd")(0).Text
c.offset(2, i) = rating.getElementsByTagName("Rating")(0).Text
i = i + 1
If i >= MAX_YEARS Then Exit For 'exit if processed enough nodes
Next rating
I'm attempting to:
open a specific URL & pass log-in information
grab data from Excel and search specified data
once search is complete, manipulate a data field to correlating Excel data and execute several commands within the application
close IE or loop search for next cell in data
I've attempted using VBA forms and modules.
I found this code online which seemed to have worked once to pass my credentials, but I can't get it to work again.
These Objects all.email & all.password would be found in the source code on the webpage as the ID?
HTMLDoc.all.Email.Value = "email#example.com"
HTMLDoc.all.Password.Value = "ex5566"
Dim HTMLDoc As HTMLDocument
Dim oBrowser As InternetExplorer
Sub Login_2_Website()
Dim oHTML_Element As IHTMLElement
Dim sURL As String
On Error GoTo Err_Clear
sURL = "example.com"
Set oBrowser = New InternetExplorer
oBrowser.Silent = True
oBrowser.timeout = 60
oBrowser.navigate sURL
oBrowser.Visible = True
Do
' Wait till the Browser is loaded
Loop Until oBrowser.readyState = READYSTATE_COMPLETE
Set HTMLDoc = oBrowser.Document
HTMLDoc.all.Email.Value = "email#example.com"
HTMLDoc.all.Password.Value = "ex5566"
For Each oHTML_Element In HTMLDoc.getElementsByTagName("input")
If oHTML_Element.Type = "submit" Then oHTML_Element.Click: Exit For
Next
' oBrowser.Refresh ' Refresh If Needed
Err_Clear:
If Err <> 0 Then
Err.Clear
Resume Next
End If
End Sub
I think you can use the same code, which you use for finding the submit button, to find the e-mail and password elements. If you know which name or id these elements have (by checking the html code of the page), you can use for instance If oHTML_Element.Name = "password" then oHTML_Element.Value = "ex5566"
If the specific elements have an ID, you can also go directly to them by using oHTML_Element = document.getElementById("[id of element]")
oHTML_Element.Value = "password" This can also be done if they don't have an id, but only a name, but then you have to find out if the name is used multiple times.
The web developer can name their inputs, buttons, forms, ids whatever they want. The email could be named Email, or ID, or Username, or XYZ, this is why you must inspect the elements in the website so you can build your code accordingly. Lets take twitter for example.
<input class="js-username-field email-input js-initial-focus" type="text" name="session[username_or_email]" autocomplete="on" value="" placeholder="Phone, email or username">
The tag is an input tag, with a class name of js-username-field email-input js-initial-focus there is no ID on it, therefore you can not use HTMLDoc.getElementByID, you have to use HTMLDoc.getElementsByClassName or you could use HTMLDoc.getElementsByTagName but if there are more than 1 input you have to loop them and correctly detect the one you need.
Its easier than it sounds but you have to have some basic knowledge of HTML. Continuing with twitter, the tag for the password is:
<input class="js-password-field" type="password" name="session[password]" placeholder="Password">
Different class and different name to differentiate between the two. And finally the login/submit button:
<button type="submit" class="submit EdgeButton EdgeButton--primary EdgeButtom--medium">Log in</button>
With these 3 portions of the HTML elements, you can log in the following way:
HTMLDoc.getElementsByClassName("js-username-field email-input js-initial-focus")(0).Value = "email#example.com"
HTMLDoc.getElementsByClassName("js-password-field")(0).Value = "ex5566"
HTMLDoc.getElementsByClassName("submit EdgeButton EdgeButton--primary EdgeButtom--medium")(0).Click
What does the (0) mean? in HTML you can have many tags with the same class name, and they all are on an array when you call getElementsByClassName, since the login site only has 1 tag with those class names, the array position of "0" is the one you are looking for.
Again, the developer can name the class, the id, anything they want, therefore you want to inspect the website to properly code your script.
I am working on a userform to load Images over labels, I am getting a
Run time error '75': Path/File access error
with below code,
dim solArr as variant
solArr = Split("1.jpg,2.jpg,3.jpg",",")
For i = LBound(solArr) To UBound(solArr)
'For rating image
Dim ratingImageName As String
ratingImageName = "D:\somepath" & "\" & solArr(i)
Set imageStar = UserForm1.Frame3.Controls.Add("Forms.Label.1")
imageStar.Top = 40 + (i * 50)
imageStar.Left = 420
imageStar.Height = 20
imageStar.Width = 100
imageStar.Picture = LoadPicture(ratingImageName)
Next
But, if i use ratingImageName as "D:\Somepath\1.jpg" no error is recieved...
Is there a better way to do it?
Hmmm.. solArr = Array("1.jpg","2.jpg","3.jpg")
I was picking up a value from cell as
1.jpg
2.jpg
3.jpg
the sentence replace(arrSol(i),chr(10),"") solved the problem.
Set imageStar = UserForm1.Frame3.Controls.Add("Forms.Label.1")
I have an array of many items in-game. Example item1, item2, item3... How to change index at item (Exemple item & i) and add a picture it item in label in Form.
I am writing an Excel macro to fill out a form on a website. I have written the code that populate the text boxes easily enough, and found code to chose radio boxes, but I am having problems with choosing info from dropdown menus.
Example 'Gender':
The combo box has three options:
Select / Male / Female
I've tried a few variations on this:
doc.getElementsByName("xs_r_gender").Item(0).Value="Male"
...but with no luck.
This is the web source code:
<td> <select name="xs_r_gender" id="xs_r_gender">
<option value="" selected>Select</option>
<option value="male">Male</option>
<option value="female">Female</option> </select></td>
Thanks.
doc.getElementById("xs_r_gender").selectedindex=1
seems to do the trick. (Where 1 represents male)
Though it means I will need to do alot of lookups to determine what the value is for the items in my dropdown. (Easy enough for Sex, where there are only two options, but I have some comboboxes with up to 50 options). If anyone knows of a faster solution, that'd be great. In the meantime, Ill start doing up some tables!!!
thanks.
Try below code assuming doc = ie.document
doc.getElementById("xs_r_gender").value = "Male"
Use this in your code to call the function below.
xOffset = SetSelect(IE.Document.all.Item("shipToStateValue"), "Texas")
doc.getElementById("shipToStateValue").selectedindex = xOffset
Then use this for your function
Function SetSelect(xComboName, xComboValue) As Integer
'Finds an option in a combobox and selects it.
Dim x As Integer
For x = 0 To xComboName.options.Length - 1
If xComboName.options(x).Text = xComboValue Then
xComboName.selectedindex = x
Exit For
End If
Next x
SetSelect = x
End Function
Thanks Stack, works for me! My solution to operate an IE HTML combobox drop down turned out to be two parts.
Part 1 was to click the pull down, here's code:
Dim eUOM1 As MSHTML.HTMLHtmlElement
Set eUOM1 = ie.document.getElementsByTagName("input")(27).NextSibling
eUOM1.Focus
eUOM1.Click
Part 2 was to choose and click the value, like this (*actual element name changed):
Dim eUOM2 As MSHTML.HTMLHtmlElement
Set eUOM2 = ie.document.getElementsByName("[*PutNameHere]")(0)
eUOM2.Value = "EA"
eUOM2.Click
Here are references:refs
You can try the querySelector method of document to apply a CSS selector of option tag with attribute value = 'male':
doc.querySelector("option[value='male']").Click
or
doc.querySelector("option[value='male']").Selected = True
Function SetSelect(s, val) As Boolean
'Selects an item (val) from a combobox (s)
'Usage:
'If Not SetSelect(IE.Document.all.Item("tspan"), "Custom") Then
'something went wrong
'Else
'continue...
'End If
Dim x As Integer
Dim r As Boolean
r = False
For x = 0 To s.Options.Length - 1
If s.Options(x).Text = val Then
s.selectedIndex = x
r = True
Exit For
End If
Next x
SetSelect = r
End Function
Try this code :
doc.getElementById("xs_r_gender").value = "Male"
doc.getElementById("xs_r_gender").FireEvent("onchange")
You can do something like this:
doc.getElementsByName("xs_r_gender").Item(1).Selected=True
or
doc.getElementById("xs_r_gender").selectedindex = 1
Where 1 is the male option (in both cases).
If the dropbox needs to fire some event in order to aknowledge your choice, it is likely that it will be the "onchange" event. You can fire it like so:
doc.getElementById("xs_r_gender").FireEvent("onchange")
If you ever want to be able to select an option based on the option's text you can use the function given by Lansman (here) .
Based on the same answer, if you want to call the option by it's value property (instead of the text, you can just change the line If xComboName.Options(x).Text = xComboValue Then to If xComboName.Options(x).value = xComboValue Then).
This should cover all bases.
Copy from Here till last line:
Sub Filldata()
Set objShell = CreateObject("Shell.Application")
IE_count = objShell.Windows.Count
For X = 0 To (IE_count - 1)
On Error Resume Next ' sometimes more web pages are counted than are open
my_url = objShell.Windows(X).document.Location
my_title = objShell.Windows(X).document.Title
If my_title Like "***Write your page name***" Then
Set IE = objShell.Windows(X)
Exit For
Else
End If
Next
With IE.document.forms("***write your form name***")
' Assuming you r picking values from MS Excel Sheet1 cell A2
i=sheet1.range("A2").value
.all("xs_r_gender").Item(i).Selected = True
End with
End sub