I searched for this with no luck. Any help is appreciated.
Data Set
I have a fairly tall dataset (roughly between 1,000 and 5,000 rows) with two columns; one for category and the other for values. The data varies; there might be 10 categories, maybe 100, or any other number:
| Column A | Column B |
|---|---|
| Category 1 | Value 1 |
| Category 1 | Value 2 |
| Category 1 | Value 3 |
| Category 2 | Value 4 |
| Category 2 | Value 5 |
| Category 2 | Value 6 |
| Category 3 | Value 7 |
| Category 3 | Value 8 |
| Category 3 | Value 9 |
| Category 3 | Value 10 |
etc.
Objective
I would like to create ranges based on the values in Column A that only include the values in Column B, using the value of Column A as the name of the range.
So for example:
- Range Name = "Category 1" ; Range Values { Value 1, Value 2, Value 3}
- Range Name = "Category 2" ; Range Values { Value 4, Value 5, Value 6}
- Range Name = "Category 3" ; Range Values { Value 7, Value 8, Value 9, Value 10}
- etc.
Things Attempted
I've tried a number of things, but I'm such a VBA novice that I can't tell what is an effective way to approach this solution and fear I keep making wrong turns in the approach.
I thought perhaps trying to find the next distinct value in Column A might be a good idea paired with the offset function, but I can't get much further than simply creating and naming the range of categories to look through in Column A. I'm sure this isn't necessary, but I am definitely not a VBA expert.
Sub AllCategories
Dim sht As Worksheet
Dim lrow As Long
Dim r As Range
Set sht = Sheets("Sheet1")
lrow = sht.Cells(Rows.Count, "A").End(xlUp).Row
Set r = sht.Range("A2:A" & lrow)
ActiveWorkbook.Names.Add _
Name:="AllCategories", _
RefersTo:=r
End Sub
Ideas?
It seems like I'll need a start variable and end variable for the categories, but I can't find how to look for the next distinct value. Maybe some Find variable function that loops?
Thanks for any help, it is much appreciated.
Cheers, Prophet

