1

I am trying to change the drop down value of a field in IE using VBA but the value is not getting updated in IE.

Below is my code:

IE.Document.getelementbyid("ddlSchemeCategory").Value = "MANAGE CASH"
Set objButton = IE.Document.getelementbyid("ddlSchemeCategory")
objButton.Focus
objButton.Click

HTML Code for the field I am trying to change:

<div class="selectbg">
  <div class="selectedvalue" id="divscat">All Categories</div>
    <select name="ddlSchemeCategory" tabindex="4" class="ddlSchemeCategory" id="ddlSchemeCategory" style="display: inline-block;">
      <option value="-1">All Categories</option>
      <option value="CREATE WEALTH">CREATE WEALTH</option>
      <option value="MANAGE CASH">MANAGE CASH</option>
      <option value="NFO">NFO</option>
      <option value="STABLE INCOME">STABLE INCOME</option>
      <option value="RETIREMENT">RETIREMENT</option>
      <option value="SAVE TAX">SAVE TAX</option>
    </select>                              
</div>

I have also tried using SelectIndex but that also did not workout for me.

2 Answers 2

1

You can also try these options below:

Sub firstOption()


Dim slct As HTMLSelectElement
Set slct = ie.document.getElementById("ddlSchemeCategory")
Dim opt


For Each opt In slct.getElementsBytagname("option")
    If opt.Value = "CREATE WEALTH" Then 'CREATE WEALTH sample value to be selected
        opt.Selected = True
        slct.fireevent "onchange"
        Exit For
    End If
Next opt

End Sub


Sub secondOption()


Dim slct As HTMLSelectElement
Set slct = ie.document.getElementById("ddlSchemeCategory")
Dim opt

Dim evt
Set evt = ie.document.createEvent("HTMLEvents")
evt.initEvent "change", True, False
For Each opt In slct.getElementsBytagname("option")
    If opt.Value = "CREATE WEALTH" Then
        opt.Selected = True
        slct.dispatchEvent evt
        Exit For
    End If
Next opt

End Sub
Sign up to request clarification or add additional context in comments.

1 Comment

thanks so much, option 2 worked for me as well. Brilliant!
0

you can try make the change with attribute = value selector for the options and then FireEvent. You might also consider attaching a change event to the select element.

ie.document.querySelector("[value='CREATE WEALTH']").Selected = True
ie.document.querySelector("#ddlSchemeCategory").FireEvent "onchange"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.