How to manipulate a specific drop down list with VBA Excel?

I have a web dropdown on the intranet that I can’t seem to manipulate in VBA here is a similar code example

HTML code:

<select id="select1">
<option value="1" selected="selected">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select> - 
<select id="select2" disabled="disabled">
<option value="1" selected="selected">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>

Here is the link of the example under: https://codepen.io/Mahdiweb/pen/OJwVoRp

I tried with the following method but without success

Application.Wait Now + TimeValue("00:00:01")
.document.getElementById("select_1").Value = "2"
Application.Wait Now + TimeValue("00:00:05")
.document.getElementById("select_2").Value = "1"

It’s been… a VERY long time since i did VBA, but why is there a dot before document? Shouldnt you have like… your application there?

2 Likes

This doesn’t seem to match either.

2 Likes

Thank you for your questions and remarks, for the question of @m_hutley
with VBA it works either with ie.document or even without the ie but the point is necessary and for the remark of @PaulOB yes you are right but even with the id=“select1” it does not work.
I have a javascript code that works fine but I don’t know how to convert it or run it in vba, here it is if you have any idea please:

const select1 = document.getElementById(‘select1’);
const select2 = document.getElementById(‘select2’);

select1.addEventListener(‘change’, () => {
select2.removeAttribute(‘disabled’);
});

setTimeout(() => {
select1.value = 2;
select1.dispatchEvent(new Event(“change”));
}, 1E3);

Thanks to everyone who tried to help me here is the solution :

Set evt = .Document.createEvent("HTMLEvents")
evt.initEvent "change", True, False
Set lst = .Document.getElementById("select1")
lst.selectedIndex = 2
lst.dispatchEvent evt
Set lst = .Document.getElementById("select2")
lst.selectedIndex = 1
lst.dispatchEvent evt
1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.