How to choose a select-list option using VBA macro ?


T

tmp2100

I have an excel VBA macro to access a website, fill in some
fields of a form, and then submit it. The macro works fine, but I am stuck on
how
to formulate a statement to choose a 'select-list' option:

Sub XYZDataFetch()
Dim ie As Object

Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
ie.navigate http://websitename_goes_here.com

Do Until ie.ReadyState <> 1: DoEvents: Loop

' ******** here is where the statement goes to choose select-list option
*******
ie.document.all.Item("cstep").Click
ie.document.all.Item("request").Value = "Summary Table"
ie.document.forms(0).submit

' more code goes here
End Sub

The webpage I am accessing is written in javascript and contains
the following select list object:

<select name="tspan" onChange="ckCustomSpanVisible();">
<option>All times</option>
<option>Last 15 mins</option>
<option>Last hour</option>
<option selected="selected">Last 4 hours</option>
<option id="custom">Custom</option>
</select>

QUESTION: at the indicated location in my macro VB code, what is the
statement
needed to force the select option away from the default 'Last 4 hours' and to
select 'Custom' instead ?

thanks,
tmp2100
 
Ad

Advertisements

T

Tim Williams

Try this function.

Tim

'********************************

Function SetSelect(s,val) as boolean
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

'*********************************
usage:


if not SetSelect(ie.document.all.Item("tspan"),"Custom") then
'something went wrong
else
'continue...
end if
 
Ad

Advertisements

T

tmp2100 via OfficeKB.com

this works (for example):
ie.document.all.Item("tspan").selectedIndex = 8
ie.document.getElementById("customSpan2").Style.visibility = "visible"

tmp2100


=============================================================

Tim said:
Try this function.

Tim

'********************************

Function SetSelect(s,val) as boolean
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

'*********************************
usage:

if not SetSelect(ie.document.all.Item("tspan"),"Custom") then
'something went wrong
else
'continue...
end if
I have an excel VBA macro to access a website, fill in some
fields of a form, and then submit it. The macro works fine, but I am stuck on
[quoted text clipped - 37 lines]
thanks,
tmp2100
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top