Error by creating a named range

HJA14

Board Regular
Joined
Apr 12, 2016
Messages
60
Hi all,


I have a file with all data in column A. In addition, I have a number of unique identifiers in column G. I would like to group all the data in column A based on the identifiers in column G.
The goal is to get multiple named ranges where each range consist of the cells that have a value that is equal to the unique identifier in column G. In addition, this unique identifier is also the name of the corresponding range.

I wrote the following macro to fulfill my needs

Code:
Sub createRanges()


Dim LastRowAll, LastRowUnique As Integer
Dim x, y
Dim rng As Variant
Dim rng_name As String


Range("I3").Select
Selection.End(xlDown).Select
LastRowUnique = ActiveCell.Row


Range("B2").Select
Selection.End(xlDown).Select
LastRowAll = ActiveCell.Row



For Each x In Range("I3:I" & LastRowUnique)
    For Each y In Range("B2:B" & LastRowAll)
        
        If IsEmpty(rng) And y = x Then
            rng = y.Address(0, 0)
        ElseIf y = x Then
            rng = rng & "," & y.Address(0, 0)
        End If
    Next y
    
    rng_name = "rng_" & x.Value
    ThisWorkbook.Names.Add Name:=rng_name, RefersTo:=rng
    'Range(rng).Name = rng_name
    rng = Empty
Next x


End Sub

This works perfectly until x becomes "20171229". I am not sure why all the other values work, but this one doesn't. There is no other range called "rng_20171229". In addition, if I create the named range manually and call it like this, it also works. Other ranges such "rng_20160715" and "rng_20160722" in the loop earlier works. Please let me know, thank you
 
Last edited:

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
How many cells are in the range you want to name?

Are the ranges you want to name contiguous or made up of several contiguous ranges?

Does this work?
Code:
Sub createRanges()
Dim LastRowAll As Long, LastRowUnique As Long
Dim x, y
Dim rng As Range
Dim rng_name As String

    LastRowUnique = Range("I3").End(xlDown).Row

    LastRowAll = Range("B2").End(xlDown).Row

    For Each x In Range("I3:I" & LastRowUnique)
        For Each y In Range("B2:B" & LastRowAll)

            If rng Is Nothing And y = x Then
                Set rng = y
            ElseIf y = x Then
                Set rng = Union(rng, y)
            End If
        Next y

        rng_name = "rng_" & x.Value
        ThisWorkbook.Names.Add Name:=rng_name, RefersTo:=rng.Address
        'Range(rng).Name = rng_name
        Set rng = Nothing
    Next x

End Sub
 
Upvote 0
How many cells are in the range you want to name?

Are the ranges you want to name contiguous or made up of several contiguous ranges?

Does this work?
Code:
Sub createRanges()
Dim LastRowAll As Long, LastRowUnique As Long
Dim x, y
Dim rng As Range
Dim rng_name As String

    LastRowUnique = Range("I3").End(xlDown).Row

    LastRowAll = Range("B2").End(xlDown).Row

    For Each x In Range("I3:I" & LastRowUnique)
        For Each y In Range("B2:B" & LastRowAll)

            If rng Is Nothing And y = x Then
                Set rng = y
            ElseIf y = x Then
                Set rng = Union(rng, y)
            End If
        Next y

        rng_name = "rng_" & x.Value
        ThisWorkbook.Names.Add Name:=rng_name, RefersTo:=rng.Address
        'Range(rng).Name = rng_name
        Set rng = Nothing
    Next x

End Sub

It works! Thank you!
 
Upvote 0

Forum statistics

Threads
1,223,907
Messages
6,175,301
Members
452,633
Latest member
DougMo

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