Loop Through a Dynamic Named Range with Excel VBA
Question details
Loop through a workbook-level dynamic named range and update cells relative to a matching value.

- 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.
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).
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.
In Excel, open Developer > Visual Basic, then open the procedure that contains the For Each 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
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.

Resolve the name from its worksheet when appropriate
Use this approach when you know the worksheet containing the range and need to check for a worksheet-scoped name.
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.

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.




