Dynamically Assign Named Ranges Based on Cell Values

Richynero

Board Regular
Joined
Jan 16, 2012
Messages
148
Hi there,

I have data structured as below with the first value in cell A1.

DogBobby
DogDave
DogButch
DogChip
CatFelix
CatTabatha
MouseRuby
MouseMark

<tbody>
</tbody>


I would like to set the range B1:B4 to a named range called 'Dog'
I would like to set the range B6:B7 to a named range called 'Cat'
I would like to set the range B8:B9 to a named range called 'Mouse'

My vba is ok but I am struggling to work out how to script the resizing of ranges based on the animal name in column A.

My real world data is several hundred rows and likely to change month on month so setting this manually, while doable, is not feasible for any long term solution.

Any help someone can give would be amazing!

Thank you

Richard
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.

Richynero

Board Regular
Joined
Jan 16, 2012
Messages
148
Hi guys,

I solved it! **Crowd goes wild***

I added a helper column to column C which was a simple COUNTIF() to tell me how many rows I'd need for my named range.


Dog Bobby 4
Dog Dave 4
Dog Butch 4
Dog Chip 4
Cat Tabatha 2
Cat Felix 2
Mouse Ruby 2
Mouse Mark 2

Code:
Sub CreateNamedRanges()


i = 1


  Do Until i > 8


      shift = Cells(i, 3).Value

      ActiveWorkbook.Names.Add Name:=Cells(i, 1).Value, RefersTo:="=LUD!$B$" & i & ":$B$" & shift + i - 1

      i = i + shift


  Loop


End Sub
 

Watch MrExcel Video

Forum statistics

Threads
1,129,774
Messages
5,638,268
Members
417,019
Latest member
PKDP

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
Top