VBA & Macro Problems

How to Simplify Repeated Application.Match Calls in VBA

Kushani NimanthikaKushani Nimanthika Oct 26, 2026 868 views

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.

How to Simplify Repeated Application.Match Calls in VBA
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.
Before you start

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.

Solution 1Recommended

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.

1
Open the VBA Editor

Press ALT + F11 on your keyboard to open the Visual Basic Editor and locate the module containing your repeated Application.Match code.

2
Insert the With Statement

Before the block of match statements, add 'With Worksheets("Sheet1")' (replace "Sheet1" with your target worksheet name).

3
Modify the Match References

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))

4
Close the With Block

Type 'End With' after your last Application.Match call to properly close the reference block.

Use a With Block for Worksheet References
Code Efficiency: Using With blocks not only makes code easier to read but can also yield slight performance improvements as VBA evaluates the object reference only once.
Advanced Macro Support in WPS

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. 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. 2. Open Your Macro-Enabled File: Launch WPS Spreadsheet and open your existing .xlsm file containing the repetitive Application.Match code.
  3. 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.
Fully compatible with Microsoft Excel VBA syntax, objects, and macro-enabled formats (.xlsm).Built-in developer tools and debugging environment for optimizing script performance.Lightweight architecture ensures that large workbooks with complex macros load and execute swiftly.
microsoft office alternative - wps office

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)'.