I want to name ranges dynamically

Frank Teunissen 0 Reputation points

I have several data sets I want to evaluate from time to time. I want to be able to use range names in the evaluation formulas, but the data to be evaluated may vary from time to time. See below.

👁 User's image

In this case I wanted to evaluate Data3. How do I convert the ranges in column C (Ranges) into the name defined in column A(Item)?

0 comments No comments

Sign in to comment

3 answers

  1. Ashish Mathur 102K Reputation points Volunteer Moderator

    Hi,

    In cell B1, type Schadejaar. In cell A1, WBE and so on. Select A1:B15000 and go to Formulas > Create name from selection. Check the box of Top Row. Click on OK.

    Hope this helps.

    0 comments No comments

    Sign in to comment
  2. Kai-L 14,395 Reputation points Microsoft External Staff Moderator

    Dear @Frank,

    Good day, and I appreciate the detailed description of your issue. I understand that you want to dynamically create named ranges where the name comes from Column A (Item) and the actual range reference comes from Column C (Ranges). One practical way to achieve this is by using VBA. You can try the following steps:

    • Press Alt + F11 to open the VBA Editor.
    • Go to Insert > Module.
    • Paste the code below into the new module.
    • Close the editor.
    • Press Alt + F8, select CreateDynamicNamedRanges, and click Run.
    Sub CreateDynamicNamedRanges()
     
        Dim ws As Worksheet
        Dim LastRow As Long
        Dim i As Long
        Dim NameStr As String
        Dim RefStr As String
       
        Set ws = ActiveSheet   ' Or change to ThisWorkbook.Sheets("YourSheetName")
       
        LastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
       
        Application.ScreenUpdating = False
       
        For i = 2 To LastRow
       
            NameStr = Trim(ws.Cells(i, "A").Value)   ' Column A = Name
            RefStr = Trim(ws.Cells(i, "C").Value)    ' Column C = Range reference
           
            If NameStr <> "" And RefStr <> "" Then
           
                ' Clean the name so it is valid for Excel
                NameStr = Replace(NameStr, " ", "_")
                NameStr = Replace(NameStr, "-", "_")
                NameStr = Replace(NameStr, ".", "_")
               
                ' Delete existing name if it already exists
                On Error Resume Next
                ThisWorkbook.Names(NameStr).Delete
                On Error GoTo 0
               
                ' Create the named range
                ThisWorkbook.Names.Add Name:=NameStr, RefersTo:="=" & RefStr, Visible:=True
               
            End If
           
        Next i
       
        Application.ScreenUpdating = True
       
        MsgBox "Named ranges have been created/updated successfully!", vbInformation
     
    End Sub
    

    After running the macro, you should be able to use the created names in formulas, for example:

    • =SUM(Bedrag)
    • =AVERAGE(Schadejaar)
    • And similar formulas using the defined names created from Column A.

    A few notes:

    • Excel defined names cannot contain spaces. So an item such as: Diersoort Categorieën cannot be used directly as a range name unless the space is replaced, for example: Diersoort_Categorieën
    • Column A should contain the names you want to create.
    • Column C should contain valid range references.
    • If a defined name already exists, the macro will delete and recreate it with the updated reference.
    • If you change the range reference in Column C later, you can run the macro again to update the defined names.

    To verify the result, go to the Formulas tab > Name Manager and check whether the named ranges were created or updated correctly.

    👁 User's image

    I hope this information is helpful. Please try the steps above and let me know how it goes. If you have any questions or need further assistance, please feel free to share them in the comments so I can continue to support you. Thank you for your patience and understanding. I'm looking forward to your reply.


    If the answer is helpful, please click "Accept Answer" and kindly upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread. 

    0 comments No comments

    Sign in to comment
  3. AI answer

    Use defined names together with a dynamic formula (for example OFFSET) and then refer to those names in the evaluation formulas.

    1. Create a dynamic defined name for each item
      1. On the Formulas tab, in Defined Names, select Define Name.
      2. In Name, type the item name (for example Bedrag).
      3. In Refers to, enter a formula that returns the range for that item on the current data sheet. For example, if Bedrag is in column G and the data start in row 2, a pattern like this can be used:
         =OFFSET(Data3!$G$2,0,0,COUNTA(Data3!$G:$G)-1,1)
        
        This makes Bedrag a dynamic range that grows or shrinks with the data in column G on sheet Data3.
      4. Repeat for each item (Schadejaar, Provincie, etc.), adjusting the column reference.
    2. Use the names in formulas After the names are defined, use them directly in formulas instead of the explicit ranges. For example:
       =SUM(Bedrag)
       =AVERAGE(Schadejaar)
      
      Excel resolves Bedrag, Schadejaar, and the other item names to the dynamic ranges defined in step 1.
    3. Switch to another data sheet To evaluate a different data set (for example Data2 instead of Data3), edit the defined names in Formulas > Name Manager and change only the sheet reference in the Refers to formula (for example, from Data3!$G$2 to Data2!$G$2). The evaluation formulas that use the names do not need to change.

    This approach converts the ranges in column C into reusable names from column A and keeps them dynamic as the underlying data changes.


    References:

    AI-generated content may be incorrect. Read our transparency notes for more information.
Sign in to answer

Your answer