How to Simplify Repeated Application.Match Calls in VBA
Question details
The user wants to optimize and clean up VBA code that uses multiple Application.Match statements to find worksheet columns and rows by their header names.

- Product
- Excel / WPS Spreadsheet
- Device & OS
- not provided
- Scenario
- Writing or maintaining VBA macros that rely on repeatedly locating multiple headers across different worksheets.
- Observed behavior
- The macro code becomes cluttered, repetitive, and difficult to maintain due to multiple Application.Match statements containing long, full worksheet object references.
Ensure you have the Developer tab enabled in your spreadsheet application and always back up your macro-enabled workbook (.xlsm) before modifying the VBA code.
Use a With Block for Worksheet References
Streamline your Application.Match calls by wrapping them in a With block, significantly reducing repetitive typing of worksheet references and improving code readability.
By using a With block, you tell VBA to apply all subsequent operations starting with a period (.) to the specified object. This removes the need to write out the full worksheet name repeatedly for every Match execution.
Press ALT + F11 on your keyboard to open the Visual Basic Editor and locate the module containing your repeated Application.Match code.
Before the block of match statements, add 'With Worksheets("Sheet1")' (replace "Sheet1" with your target worksheet name).
Inside the block, replace the full worksheet references with a leading period. For example, change it to: value = .Cells(sysrow, Application.Match("Header", .Range("A1:Z1"), 0))
Type 'End With' after your last Application.Match call to properly close the reference block.

Store Headers in an Array and Iterate via a Loop
If you are searching for many variables or headers, place the header names in an array and use a loop to process them systematically.
Run and Edit VBA Macros Seamlessly in WPS Spreadsheet
WPS Spreadsheet offers a robust, built-in VBA editor, making it incredibly simple to write, run, and optimize complex scripts like arrays and Application.Match functions without any extra configuration.
- 1. Download and Install WPS Office: Get the latest version of WPS Office for free from the official website and install it on your device.
- 2. Open Your Macro-Enabled File: Launch WPS Spreadsheet and open your existing .xlsm file containing the repetitive Application.Match code.
- 3. Use the Built-in VBA Editor: Navigate to the Developer tab, click on the VBA Editor, and apply With blocks or Arrays to streamline your code instantly.

Frequently Asked Questions
What happens if Application.Match does not find the target header?
If a match is not found, Application.Match will return an error (Error 2042). To prevent the macro from crashing, you should assign the result to a Variant variable and use the IsError() function to check if the match was successful before using the column index.
What exactly do LBound and UBound do in VBA?
LBound and UBound are built-in VBA functions that return the lowest (lower limit) and highest (upper limit) available subscripts of an array. They are highly recommended for safely looping through all elements in an array, especially when the size of the array might change dynamically.
Can I use Named Ranges to avoid Application.Match entirely?
Yes. If your specific headers are already defined as Named Ranges in the spreadsheet (e.g., 'CohLen'), you do not need to use Application.Match. You can locate the column directly using an expression like 'Cells(sysrow, Range("CohLen").Column)'.




