Use VBA Variables with Excel Names.Add for Year-Based Ranges
Question details
Create Excel defined names with VBA variables for the year, month, worksheet, and cell range.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- The user needs names such as Mth_15_01_amt for a range on Data 2015 and equivalent names for Data 2016.
- Observed behavior
- The desired result is a set of defined names that refer to the corresponding ranges on each year's worksheet.
Confirm that the Data 2015 and Data 2016 worksheets exist in the workbook containing the macro. Identify the cell range for each month and check whether the names should be workbook-scoped or worksheet-scoped.
Build workbook-scoped names with VBA variables
Use Names.Add to create equivalent names for the same example range on the 2015 and 2016 worksheets.
The macro builds each defined name and its RefersTo formula as strings. It places the worksheet name in single quotes and doubles any apostrophes within it, so names containing spaces or apostrophes are handled correctly.
In Excel, open the workbook containing the Data 2015 and Data 2016 sheets, then select Developer > Visual Basic. In the VBA editor, select Insert > Module.
Paste this code into the module: Sub AddYearBasedNames() Dim yearValue As Long Dim monthValue As Long Dim sheetName As String Dim rangeAddress As String Dim definedName As String Dim refersToFormula As String Dim ws As Worksheet monthValue = 1 rangeAddress = "$B$2:$B$32" ' Replace with the required range. For yearValue = 2015 To 2016 sheetName = "Data " & CStr(yearValue) Set ws = ThisWorkbook.Worksheets(sheetName) definedName = "Mth_" & Format$(yearValue Mod 100, "00") & _ "_" & Format$(monthValue, "00") & "_amt" refersToFormula = "='" & Replace(ws.Name, "'", "''") & _ "'!" & ws.Range(rangeAddress).Address ThisWorkbook.Names.Add Name:=definedName, RefersTo:=refersToFormula Next yearValue End Sub
In the module, change monthValue and rangeAddress to match the range you need. The example uses the same January range on both sheets; use the appropriate address for each month if ranges differ.
Run AddYearBasedNames from the VBA editor, then open Formulas > Name Manager in Excel. Check that Mth_15_01_amt and Mth_16_01_amt refer to the intended ranges.

Work with Excel spreadsheets in WPS Office
WPS Office is a free, lightweight Office alternative with a familiar interface. Open compatible Excel files in WPS Spreadsheets for an easier move between office suites; it does not fix Excel's VBA behavior.

Frequently Asked Questions
Why does the worksheet name need single quotes?
A RefersTo formula needs single quotes around sheet names containing spaces, such as 'Data 2015'. The macro also doubles apostrophes within a sheet name using Replace.
How can I create a name for another month?
Change monthValue in the macro and set rangeAddress to that month's cells, then run AddYearBasedNames. Format$(monthValue, "00") keeps the month at two digits.
Where can I check the names created by the macro?
In Excel, select Formulas > Name Manager. Review each name's scope and Refers To value, and edit a reference there if needed.
How do I make a name specific to one worksheet?
Use the worksheet's Names collection, such as ws.Names.Add, instead of ThisWorkbook.Names.Add. Check the Scope column in Formulas > Name Manager to confirm the result.




