VBA & Macro Problems

How to Update Every Matching Row with VBA in Excel (Avoid Global Variables)

Huda QurayshiHuda Qurayshi Oct 26, 2026 869 views

Question details

The user needs to update every matching row in a column using a VBA macro instead of just the first occurrence, while avoiding global variables.

How to Update Every Matching Row and Avoid Global Variables in VBA
Product
WPS Spreadsheet / Microsoft Excel
Device & OS
not provided
Scenario
Searching a column (e.g., column B) for a specific text string (like "Wavelength Range") and updating a corresponding offset cell for every match found without triggering object reference errors.
Observed behavior
The current macro only updates the first matching occurrence and fails to process subsequent matching rows. It may also throw an "Object doesn't support this property or method" error due to incorrectly qualified worksheet references.
Before you start

Ensure you have backed up your current macro code and saved a copy of your workbook before running loops that overwrite data in your spreadsheet.

Solution 1Recommended

Loop Through Target Range Using Range Variables

Use a 'For Each' loop with a Range variable to iterate through every cell in your target range, allowing you to identify and update all occurrences accurately.

By looping through a defined target range cell-by-cell instead of relying on basic 'Find' functions without 'FindNext', you ensure every single match is updated. Keeping variables localized prevents memory conflicts that global variables often cause.

1
Declare your variables locally

Open the VBA Editor (Alt + F11). Start your subroutine and declare your variables locally inside the sub, such as `Dim matchingCell As Range` and `Dim targetRange As Range`, to avoid using global variables.

2
Define the target search range

Set your target range to the specific column and rows you need to search. For example: `Set targetRange = ThisWorkbook.Sheets("Sheet1").Range("B1:B1000")`. Ensure worksheets are qualified correctly to avoid 'Object doesn't support property' errors.

3
Create the For Each loop

Write a loop that checks each cell by typing `For Each matchingCell In targetRange`. Follow this with an If statement to check the value: `If matchingCell.Value = "Wavelength Range" Then`.

4
Use Offset to write data

Inside the If statement, use the Offset method to write the result to the correct column. Use `matchingCell.Offset(0, 2).Value = wavelengthRange` to update the cell in the same row, two columns to the right.

5
Close the loop and test

Close the logic block with `End If` and proceed to the next iteration with `Next matchingCell`. Run the macro to verify it updates every matching row in the entire column.

Loop Through Target Range Using Range Variables
Correct Object Referencing: Fully qualifying your worksheet references (e.g., explicitly defining `ThisWorkbook.Sheets("SheetName")`) prevents the common "Object doesn't support this property or method" error when dealing with Range offsets.
Advanced Macros in WPS Spreadsheet

Easily Create and Run VBA Macros with WPS Office

WPS Spreadsheet offers highly compatible support for VBA and Macros, allowing you to run loops, define variables, and automate repetitive tasks just as you would in Microsoft Excel.

  1. 1. Download and Install: Download WPS Office for free from the official website and open your macro-enabled spreadsheet (.xlsm).
  2. 2. Enable Macros and the Developer Tab: Go to the 'Developer' tab on the top ribbon. If it's your first time, you may need to enable macros in the security settings. Click 'Visual Basic' to access your code editor.
  3. 3. Run the VBA Loop: Paste your updated 'For Each' loop code into the module. Click the 'Run' button (or press F5) to automatically process and update all matching rows in your dataset.
Fully compatible with Microsoft Excel VBA syntax and .xlsm file formats.Free, lightweight, and fast-loading spreadsheet application.Built-in Visual Basic Editor for seamless macro debugging and execution.
QA img-9

Frequently Asked Questions

Why is my VBA macro only updating the first matching row?

This typically happens if you use the `Range.Find` method without a subsequent `FindNext` loop, or if your code exits the loop prematurely after the first match. Using a `For Each` loop to iterate through the entire range ensures all cells are checked and updated.

Why should I avoid using global variables in Excel VBA?

Global variables remain in memory as long as the workbook is open and can be unintentionally modified by other subroutines. This can lead to unpredictable results, logic bugs, and memory bloat. Declaring variables locally within the subroutine is a safer and cleaner coding practice.

What does the 'Object doesn't support this property or method' error mean in this context?

In this specific context, it usually means your Range variable is incorrectly qualified with a Worksheet object (e.g., mismatched data types or incorrect syntax like `Worksheets(sysnum)` without a proper Object `Set` declaration). Verify that your Range variables are properly defined and assigned.

How does the Offset property work in VBA?

The `Offset(RowOffset, ColumnOffset)` property allows you to reference a cell relative to your current Range. For instance, `matchingCell.Offset(0, 2)` refers to the cell located in the exact same row (0) but two columns to the right (2) of the current cell.