create multiple dynamic named ranges using vba

palaeontology

Active Member
Joined
May 12, 2017
Messages
444
Office Version
  1. 2016
Platform
  1. Windows
Hi all,

I know how to make dynamic named ranges using the ws ribbon functions 'Formulas' and 'Define Name', however I have a large number of spreadsheet, and each spreadsheet will need a large number of dynamic named ranges.

My VBA coding is poor, at best, so I'm wondering if it's possible to have code create these numerous dynamic named ranges automatically.

The coding would only need to look at the ws called .... Entry

The 'Entry' ws might look something like this ...

mr excel explanation photo.JPG


In the image above, there is a maths class that goes by the code .... 10MAA501 ... there are other classes in this Entry ws, for example 10MAA502, 10MAA503, 10MAA601, 10MAA602, 10MAA701, 10MAA702 etc etc etc

From time to time, new students will be added to some of the classes, so I need some code to create a dynamic named range for each class in the list.

So for example, the 10MAA501 range of data currently spans the range .... Entry!A8:AH34 .... and the 10MAA502 range of data currently spans the range .... Entry!A35:AH60 but when new students are added, the ws is re-sorted by column C, so the range for a class would change to something else, for example, 10MAA501 might change to Entry!A8:AH37, and 10MAA502 might change to Entry!A38:AH67.

Is VBA able to create dynamic named ranges or will I need to do each one by hand .... a task I'm not looking forward to and would need to redo for each of 23 workbooks every 6 months.

Kind regards,

Chris
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Try this:
It won't create a dynamic range but it will redefine the range of each named range.
I assumed Data is in col A:AH, start at row 8.
When you finish adding the class code (in col C), sort the data by col C, then run this code. It will create/recreate the named range, the range of each named range will change accordingly.
VBA Code:
Sub a1117315a()
'https://www.mrexcel.com/board/threads/create-multiple-dynamic-named-ranges-using-vba.1117315/

Dim i As Long, j As Long
Dim va

va = Range("C1", Cells(Rows.count, "C").End(xlUp))
For i = 8 To UBound(va, 1)
    j = i
    Do
        i = i + 1
        If i > UBound(va, 1) Then Exit Do
    Loop While va(i, 1) = va(i - 1, 1)
    i = i - 1
    ActiveWorkbook.names.Add Name:=va(i, 1), RefersTo:=Range(Cells(j, "A"), Cells(i, "AH"))

Next

End Sub
 
Upvote 0
Hi Akuini, thankyou, so much, for putting that together for me, but I'm getting the following error message ... Run-time error '1004' : Application-defined or object-defined error

It's not highlighting any particular part of your code, so I'm not sure what I'm doing wrong.

Since trying your code, I made the first 3 dynamic named ranges manually and recorded the code needed ....

Code:
Sub Dynamic_10MAA501()
    Range("A6").Select
    ActiveWorkbook.Worksheets("Sheet1").Names.Add Name:="Dynamic_10MAA501", _
        RefersToR1C1:="=OFFSET(Sheet1!R6C1,1,0,COUNTIF(Sheet1!C3,""10MAA501""),34)"    'the named range will refer to columns starting at column A and finishing at column AH, and will refer to as many rows (starting at row 7) as relate to 10MAA501 (as found in column C)'
    ActiveWorkbook.Worksheets("Sheet1").Names("Dynamic_10MAA501").Comment = ""
    ActiveWorkbook.Worksheets("Sheet1").Names.Add Name:="Dynamic_10MAA502", _
        RefersToR1C1:= _
        "=OFFSET(Sheet1!R6C1,COUNTIF(Sheet1!C3,""10MAA501"")+1,0,COUNTIF(Sheet1!C3,""10MAA502""),34)"    'the named range will refer to columns starting at column A and finishing at column AH, and will refer to as many rows (starting at the row immediately after 10MAA501) as relate to 10MAA502 (as found in column C)'
    ActiveWorkbook.Worksheets("Sheet1").Names("Dynamic_10MAA502").Comment = ""
    ActiveWorkbook.Worksheets("Sheet1").Names.Add Name:="Dynamic_10MAA503", _
        RefersToR1C1:= _
        "=OFFSET(Sheet1!R6C1,COUNTIF(Sheet1!C3,""10MAA501"")+COUNTIF(Sheet1!C3,""10MAA502"")+1,0,COUNTIF(Sheet1!C3,""10MAA503""),34)"    'the named range will refer to columns starting at column A and finishing at column AH, and will refer to as many rows (starting at the row immediately after 10MAA502) as relate to 10MAA503 (as found in column C)'
    ActiveWorkbook.Worksheets("Sheet1").Names("Dynamic_10MAA503").Comment = ""
    ActiveWindow.SmallScroll Down:=-20
End Sub

As you can see, each new dynamic range requires knowing what row the previous dynamic range finished on, then beginning on the very next row.

The Math class codes won't always be 10MAA501 followed by 10MAA502, etc etc etc. Each 6 months they will be a different set of codes.

Is there a way to shortcut the length and complexity of my code, so I'm not having to go into each one and making the first of the countifs longer and longer each time ?

Kind regards,

Chris
 
Upvote 0
Ah, sorry, I didn't use your class code when testing the the code.
First, replace this line:
ActiveWorkbook.names.Add Name:=va(i, 1), RefersTo:=Range(Cells(j, "A"), Cells(i, "AH"))
with this:
ActiveWorkbook.names.Add Name:="Dynamic_" & va(i, 1), RefersTo:=Range(Cells(j, "A"), Cells(i, "AH"))

And if your data start at row 7 then :
Replace this line:
For i = 8 To UBound(va, 1)
with this:
For i = 7 To UBound(va, 1)

And let's try on a simple data below.
Book1
ABC
710MAA501
810MAA501
910MAA501
1010MAA503
1110MAA503
1210MAA503
1310MAA503
1410MAA503
1510MAA503
1610MAA504
1710MAA504
1810MAA504
Sheet1

Run the code on the example data, see in Name Manager, it will create the named ranges.
name_manager.jpg

Then try adding some data in col C, sort the data, then run the code again. See the changes in the Name Manager.
 
Upvote 0
That worked brilliantly Akuini, thank you so very much.

My vba is very poor, so I'll now try to understand each line of your code so I can learn from this.

Thank you, so much, again, I really am appreciative of this.

Very kind regards,

Chris
 
Upvote 0
You're welcome, glad to help, & thanks for the feedback.:)
 
Upvote 0

Forum statistics

Threads
1,214,885
Messages
6,122,085
Members
449,064
Latest member
MattDRT

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top