How to Automate Cascading Web Drop-Down Lists with Excel VBA
Question details
The user wants to automate the selection of cascading web drop-down lists (e.g., country, state, district) using Excel VBA.

- Product
- Excel
- Device & OS
- not provided
- Scenario
- Writing an Excel VBA macro to interact with a web form containing dependent drop-down lists, where selecting an option in the first list populates the second list.
- Observed behavior
- Simply setting the HTML option value using VBA does not trigger the dependent drop-down lists to populate, because the page's built-in JavaScript events are not being fired.
Ensure you have enabled the necessary object libraries in your VBA editor, such as 'Microsoft HTML Object Library' and 'Microsoft XML, v6.0', before attempting to write web interaction scripts.
Trigger the JavaScript OnChange Event
Force the webpage to recognize the new value and populate the cascading list by explicitly firing the onchange event through your VBA script.
When dealing with modern web forms, interacting with the dropdown element's value property directly isn't enough. The webpage relies on a JavaScript event (usually 'onchange' or 'click') to know when to fetch and display the sub-categories. You must simulate this event manually in your VBA code.
Locate the primary drop-down list using methods like document.getElementById or document.getElementsByName, and assign the desired value to its value property.
Call the fireEvent method on the HTML element (e.g., element.fireEvent("onchange")) or use dispatchEvent to force the webpage to trigger its underlying JavaScript functions.
Since the dependent list takes time to populate (often requiring an asynchronous server request), use Application.Wait to pause the script briefly before interacting with the newly populated cascading drop-down.

Use XMLHttpRequest to Submit Form Data Directly
Bypass the web interface entirely by submitting the necessary form data directly to the server using HTTP requests.
Automate Web Tasks with WPS Spreadsheet VBA
WPS Spreadsheet features a robust, built-in VBA editor that enables you to automate complex web scraping and data entry tasks effortlessly. Run your existing Excel macros or build new automation scripts with comprehensive object library support.
- 1. Launch WPS Spreadsheet: Open WPS Spreadsheet and navigate to the 'Developer' tab on the top ribbon.
- 2. Open the VBA Editor: Click on 'VBA Editor' to launch the integrated macro development environment.
- 3. Enable Object References: Go to 'Tools' > 'References' in the editor menu, and check 'Microsoft HTML Object Library' to support web DOM manipulation.
- 4. Run Your Automation Script: Insert a new Module, paste your cascading drop-down automation code, and press F5 to execute the script.

Frequently Asked Questions
Why does my VBA code select a drop-down value, but the next cascading drop-down remains empty?
Cascading drop-downs typically rely on JavaScript events (like 'onchange') to communicate with the server and fetch the secondary list. Merely setting the value property in VBA bypasses the user interface and fails to trigger these background scripts. You must explicitly fire the event using VBA.
How do I find the correct HTML ID or Name of a drop-down list on a webpage?
Open the target webpage in any standard web browser, right-click on the drop-down list you want to automate, and select 'Inspect' or 'Inspect Element'. This will open the developer tools where you can easily find the 'id', 'name', or 'class' attributes associated with the <select> tag.
Is using XMLHttpRequest faster than automating Internet Explorer with VBA?
Yes, XMLHttpRequest is significantly faster and more stable. It sends the HTTP request directly to the server in the background, entirely skipping the process of rendering the graphical interface, loading images, or executing unnecessary webpage scripts.
How do I pause my VBA script to wait for a cascading web list to load?
You can use the Application.Wait method to introduce a hard pause in your script (e.g., Application.Wait (Now + TimeValue("0:00:02"))). Alternatively, you can write a loop with DoEvents that checks the webpage's document readystate or waits until the target drop-down element is no longer empty.




