Create Named Range From Values in Adjacent Cells

Mayanwolfe

New Member
Joined
Jun 5, 2013
Messages
27
Hello,

I am trying to create a series of several named ranges in a single column that are defined by the values in previous columns using VBA. For example, I have one column for "colors" and another for "shades". The third column is their product numbers. I would like to group the product numbers into named ranges that are defined and named based on the concatenation of color & shade.

For example, the first range below would be named BlueLight and contain the four items in the PRODNO column associated with this combination. The next would be named BlueMedium and have only one row, and so on. I have made sure that the columns are ordered by COLOR and SHADE accordingly. Essentially, I need the macro to add cells to the range until the value of COLOR&SHADE changes.

COLORSHADEPRODNO
BlueLight
9508​
BlueLight
2595​
BlueLight
2656​
BlueLight
6407​
BlueMedium
1838​
BlueDark
3142​
BlueDark
3628​
RedLight
6787​
RedMedium
3540​
RedMedium
6103​
RedMedium
2793​
RedMedium
4004​
RedDark
4745​
RedDark
6229​
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Try this macro, which assumes your data is in columns A:C.
VBA Code:
Public Sub Create_Named_Ranges()

    Dim r As Long, lastRow As Long, startRow As Long
    Dim colourShade As String
    
    With ActiveSheet
        lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
        r = 2
        While r <= lastRow
            startRow = r
            colourShade = .Cells(r, "A").Value & .Cells(r, "B").Value
            While r <= lastRow And .Cells(r, "A").Value & .Cells(r, "B").Value = colourShade
                r = r + 1
            Wend
            ActiveWorkbook.Names.Add Name:=colourShade, RefersTo:=.Range(.Cells(startRow, "C"), .Cells(r - 1, "C"))
        Wend
    End With
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,713
Messages
6,174,038
Members
452,542
Latest member
Bricklin

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