VBA & Macro Problems

Create a VBA Loop for INDEX/MATCH Calculations in Excel

Ayan MasoodAyan Masood Oct 12, 2026 869 views

Question details

The user needs a VBA macro to perform an INDEX/MATCH style calculation by matching product criteria across worksheets and multiplying the matched percentage by a specific column's total quantity.

Create a VBA Loop for INDEX/MATCH Calculations in Excel
Product
Microsoft Excel
Device & OS
not provided
Scenario
Automating data lookup and calculation tasks that normally use complex nested formulas across multiple sheets.
Observed behavior
The user wants to replace manual formulas with a VBA loop to automatically calculate product ratios multiplied by total quantities in column D.
Before you start

Ensure you have enabled the Developer tab in your spreadsheet application and saved your workbook as a Macro-Enabled Workbook (.xlsm) to prevent losing your VBA code.

Solution 1Recommended

Use Nested For Loops to Match Criteria and Multiply Quantities

Use nested For loops in VBA to iterate through data rows, match multiple criteria, and apply calculations directly without relying on heavy worksheet formulas.

This approach mimics an INDEX/MATCH formula with multiple criteria by looping through data ranges. It compares the target row criteria against your data source and assigns the calculated result (the matched percentage multiplied by the Column D quantity) directly to the destination cell.

1
Open the VBA Editor

Press ALT + F11 to open the Visual Basic for Applications Editor. Click Insert > Module to create a new module for your macro.

2
Define Your Variables

Declare Long variables for your row counts (e.g., lr1, lr2) and loop counters (e.g., x, y, p) to handle the iteration efficiently.

3
Determine the Last Used Rows

Use the .End(xlUp).Row property to dynamically find the last used rows for both your target data and your lookup ranges, ensuring the loop processes all existing data.

4
Create the Nested Loop Structure

Write 'For x = 2 To lr1' and 'For y = 2 To lr2' to loop through the main sheet and the lookup sheet respectively.

5
Add the Matching Logic and Calculation

Use If statements to match your criteria (e.g., If .Cells(y, 14) = .Cells(x, 2)). Inside the If block, execute the calculation by multiplying the matched percentage by Column D (column index 4): .Cells(x, p) = .Cells(y, 17) * .Cells(x, 4).

6
Run the Macro

Close the VBA Editor, return to your worksheet, and run the macro by navigating to Developer > Macros and selecting your newly created script.

Use Nested For Loops to Match Criteria and Multiply Quantities
Adjusting Worksheet and Range References: Make sure to adjust the worksheet names, column indices, and criteria ranges in the VBA code to exactly match your specific workbook structure.
Advanced Spreadsheets with VBA Support

Use VBA Macros for Complex Calculations in WPS Spreadsheets

WPS Spreadsheets provides robust support for standard VBA macros, allowing you to run loops, automate lookups, and perform advanced INDEX/MATCH style data manipulation effortlessly.

  1. 1. Access the Developer Tab: Open WPS Spreadsheets and navigate to the Developer tab on the top ribbon.
  2. 2. Open the VBA Editor: Click on 'Visual Basic' or press ALT + F11 to open the built-in VBA Editor.
  3. 3. Insert and Customize Code: Insert a new module and paste your INDEX/MATCH loop code, ensuring column references match your data layout.
  4. 4. Save and Execute: Save the file as an Excel Macro-Enabled Workbook (.xlsm) and click 'Run' to execute the calculations directly within WPS Office.
Fully compatible with Microsoft Excel VBA syntax and macrosLightweight application with high performance for large datasetsFamiliar user interface for seamless migration without a steep learning curveSupports complex array calculations and nested loops natively
microsoft office alternative - wps office

Frequently Asked Questions

Can I use WorksheetFunction.Index and Match in VBA instead of looping?

Yes. You can call Application.WorksheetFunction.Index and Match directly in VBA. This is sometimes faster than looping cell-by-cell for small datasets, but loops offer more control for nested conditions and inline math calculations.

Why is my VBA loop running very slowly on large datasets?

Interacting directly with worksheet cells inside a loop is computationally heavy. To speed it up, you can read your worksheet data ranges into VBA arrays, perform the calculations in memory, and then write the final array back to the sheet in one go.

How do I multiply the matched value by a specific column in my VBA loop?

You can reference the specific column index using the .Cells(row, column) property. For instance, to multiply by Column D (which is the 4th column), simply append '* .Cells(x, 4)' to your assignment statement within the loop.

Do VBA macros in WPS Office use the same syntax as Microsoft Excel?

Yes, WPS Spreadsheets supports standard VBA syntax, meaning the vast majority of macro scripts you write for Excel will run seamlessly in WPS Office without modification.