VBA to create multiple named ranges

SPY

New Member
Joined
Nov 9, 2006
Messages
29
Hi
I need to create multiple named ranges to use in chart series.

Column C contains the name to be used for the named range and Columns D:S contain the data. Some of the rows don't have a complete range of data in D:S so would need to check for blanks.

Any help appreciated!
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
You might consider the following as a start...

Code:
Sub CreateNamedRanges()

Dim LastRow As Long
Dim r As Range

LastRow = Cells(Rows.Count, "C").End(xlUp).Row

For Each r In Range("C1:C" & LastRow)
    Range(r.Offset(0, 1), r.Offset(0, 17)).Name = r.Value
Next r

End Sub

It doesn't address "blanks" - as you didn't specify if the blanks are all at the end of the row or if they could be interspersed within the row. And in the case of the latter, would you want to move the cells to the left to eliminate the blanks? Or something else?

Cheers,

tonyyy
 
Upvote 0
Thanks Tonyyy that's brilliant!

With regards to the problem with blanks, these would be at the end of some rows and as a result the range will be shorter eg D:P when QR&S are blank
 
Upvote 0
Now adjusted for blank cells at the end of the row...

Code:
Sub CreateNamedRanges()
Application.ScreenUpdating = False

Dim LastRow As Long
Dim LastCol As Long
Dim r As Range

LastRow = Cells(Rows.Count, "C").End(xlUp).Row

For Each r In Range("C1:C" & LastRow)
    LastCol = Cells(r.Row, Columns.Count).End(xlToLeft).Column
    Range(r.Offset(0, 1), r.Offset(0, LastCol - 3)).Name = r.Value
Next r

Application.ScreenUpdating = True
End Sub

Cheers,

tonyyy
 
Upvote 0
You're welcome. Happy to help.

tonyyy
 
Upvote 0

Forum statistics

Threads
1,214,643
Messages
6,120,702
Members
448,980
Latest member
CarlosWin

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