VBA & Macro Problems

Loop Through a Dynamic Named Range with Excel VBA

Emma BrownEmma Brown Nov 25, 2026 871 views

Question details

Loop through a workbook-level dynamic named range and update cells relative to a matching value.

Loop Through a Dynamic Named Range with Excel VBA
Product
Microsoft Excel VBA
Device & OS
not provided
Scenario
A VBA For Each loop uses the dynamic named range IL_FileName, which refers to cells on the IMPORTED_LIST worksheet.
Observed behavior
Qualifying the name with a different worksheet can cause a VBA error, and references to a different cell variable can prevent the loop from working as intended.
Before you start

In Excel, open Formulas > Name Manager and check that IL_FileName has workbook scope and its formula resolves to a valid range on IMPORTED_LIST. Confirm that the range starts at least two columns to the right of column A if the macro will use Offset(0, -2).

Solution 1Recommended

Reference the workbook-level name in the For Each loop

Use the workbook's Names collection to resolve the dynamic range without qualifying it with the wrong worksheet.

The example assumes the named range is in the workbook containing the macro and that the code runs in a form or class where Me.IL_SelectVessel_L is available.

1
Open the VBA editor

In Excel, open Developer > Visual Basic, then open the procedure that contains the For Each loop.

2
Update the loop

Replace the worksheet-qualified reference with the workbook-level name and use rCell throughout the loop: Dim rCell As Range For Each rCell In ThisWorkbook.Names("IL_FileName").RefersToRange.Cells If rCell.Value = Me.IL_SelectVessel_L.Value Then rCell.Offset(0, -2).Value = "Selected" End If Next rCell

3
Run and check the macro

Run the procedure from the VBA editor or its assigned control, then check that matching rows have Selected two columns to the left of the named range.

Reference the workbook-level name in the For Each loop
Check the workbook reference: ThisWorkbook means the workbook containing the VBA code. If the name is in another workbook, replace ThisWorkbook with a reference to that workbook. RefersToRange requires the name's formula to resolve to a range.
Free Microsoft Office alternative

Work with Excel spreadsheets in WPS Office

WPS Office is a free, lightweight alternative with a familiar interface for opening and editing spreadsheets. Its Excel format compatibility makes it easier to move existing files, although VBA macro support and behavior can vary.

Open and edit Excel spreadsheets in a familiar interfaceCompatible with Microsoft Excel formats such as XLSXReview spreadsheet data and named ranges when working with imported listsFree and lightweight for everyday office tasks
microsoft office alternative - wps office

Frequently Asked Questions

Why does the worksheet-qualified named range cause an error?

The name may refer to cells on a different sheet, or a sheet-scoped name may be involved. Check Formulas > Name Manager, then use ThisWorkbook.Names("IL_FileName").RefersToRange for the workbook-level name.

How do I check whether a named range is dynamic?

Open Formulas > Name Manager and select the name. Inspect its Refers to formula and verify that it resolves to the expected cells as the source data changes.

Why should I use rCell instead of Cell inside the loop?

The For Each statement assigns each range cell to rCell. Use rCell.Value and rCell.Offset so both operations apply to the current cell.

Why does Offset(0, -2) fail for some ranges?

Offset(0, -2) refers to a cell two columns to the left. If a loop cell is in column A or B, that position is outside the worksheet. Move the range or change the offset.

Can WPS Office run this Excel VBA macro unchanged?

Macro availability and VBA compatibility can vary by WPS version and setup. Test the workbook and its macro in your WPS environment; opening an Excel file does not guarantee that its VBA code will run unchanged.