VBA & Macro Problems

How to Highlight Dependent Materials in an Excel Bill of Materials using VBA

Natalie TaylorNatalie Taylor Nov 11, 2026 870 views

Question details

The user needs to identify and highlight every lower-level dependency in a hierarchical Bill of Materials using a VBA macro.

Highlight Dependent Materials in an Excel Bill of Materials using VBA
Product
Excel
Device & OS
not provided
Scenario
Tracing multi-level dependencies in a complex Bill of Materials spreadsheet.
Observed behavior
To recursively search the Pegged Requirement and Material columns, identifying and highlighting rows corresponding to child materials through all levels.
Before you start

Before running the macro, ensure you have enabled the Developer tab in your spreadsheet and saved a backup copy of your data to prevent accidental formatting loss from recursive highlighting.

Solution 1Recommended

Create a Recursive VBA Macro to Highlight BOM Dependencies

Write a VBA script utilizing a recursive function that safely traces child materials through parent dependencies and highlights the corresponding rows.

A hierarchical Bill of Materials requires recursion to trace components from the top level down to the lowest sub-assembly. By setting up a VBA macro that prompts for a starting material, you can programmatically scan the 'Pegged Requirement' and 'Material' columns to find matches.

Since the depth of child materials can vary, the macro must call itself (recursion) whenever a new sub-component is found. It's highly important to include safeguards within your code to prevent infinite loops in case the data contains circular references.

1
Open the VBA Editor

Press ALT + F11 on your keyboard to open the Visual Basic for Applications (VBA) Editor in your spreadsheet.

2
Insert a New Module

Right-click on your workbook name in the Project Explorer window on the left, select 'Insert', and then click 'Module'.

3
Write the Recursion Script

Paste your custom VBA script into the code window. Ensure your script logic scans the target 'Material' column, searches for it in the 'Pegged Requirement' column, and highlights the row using the Range.Interior.Color property. Add an array or dictionary to track evaluated materials and provide cycle protection.

4
Execute the Macro

Close the VBA editor, return to your spreadsheet, press ALT + F8, select your new macro, and click 'Run'. Enter the parent material number when prompted.

Create a Recursive VBA Macro to Highlight BOM Dependencies
Circular Dependency Protection: When dealing with multi-level BOMs, ensure your macro tracks visited items in a Collection or Dictionary to prevent an infinite loop (Stack Overflow) caused by circular data references.
Advanced Macro Support

Manage and Execute VBA Macros Seamlessly with WPS Office

WPS Spreadsheet provides robust support for VBA macros, allowing you to run complex recursion scripts to manage and highlight your Bill of Materials just like you would in Microsoft Excel.

  1. 1. Enable Developer Tools: Open WPS Spreadsheet, go to Options, and verify that Developer Tools are enabled to access macro features.
  2. 2. Open the VBA Editor: Navigate to the Developer tab on the ribbon and click 'VBA Editor' to open the coding interface.
  3. 3. Paste Your BOM Script: Insert a new module within the editor and paste your recursive Bill of Materials script.
  4. 4. Run the Code: Return to your worksheet, click 'Macros' on the Developer tab, select your script, and execute it to highlight the dependencies.
Fully compatible with Microsoft Excel (.xlsm, .xls) macro-enabled formats.Advanced built-in VBA editor to write, debug, and execute BOM tracking recursive macros.Lightweight architecture ensuring complex scripts and large datasets run smoothly.Free alternative with a highly familiar user interface and zero learning curve.
microsoft office alternative - wps office

Frequently Asked Questions

Why does my macro crash when evaluating complex BOMs?

If your BOM contains recursive circular loops (e.g., a sub-component mistakenly requiring its own parent), standard recursion will cause a Stack Overflow. Your VBA code must include cycle protection, such as storing visited material numbers in a dictionary, to skip already processed nodes.

Why aren't the child materials being highlighted after running the macro?

Check if your macro's column references accurately match your dataset layout. If your 'Material' and 'Pegged Requirement' columns are swapped or defined incorrectly in the code (for example, referencing Column A instead of Column B), the recursive search will fail to locate the children.

Can I easily clear the highlights applied by the macro?

Yes. You can select the entire dataset, go to the Home tab, click the 'Fill Color' dropdown menu (paint bucket icon), and select 'No Fill' to remove all background colors applied by the VBA script.

Does WPS Office support VBA macros for checking dependencies?

Yes, WPS Office Pro and certain regional versions fully support VBA. You can open your existing Excel macro-enabled workbooks (.xlsm) in WPS Spreadsheet and run your BOM dependency macros without modifying the code.