Creating Named Ranges Using VBA - Run time error 1004

Prevost

Board Regular
Joined
Jan 23, 2014
Messages
198
Hi There. I am trying to create named ranges. All the range names are in the first row and below them is their respective range entries (the cell immediately below the name cell is intentionally left blank). I check the first row cells until I get to the end (where an empty cell is) to get the number of ranges. Then I search down the column, starting at the first column, until I reach an empty cell to determine the range length. There is an error when I attempt to assign the range name to the Cell containing the text. The code won't work when
[Name:= Cells(1,y)].......but I don't get an error when the code is [Name:= "String Text"].......but obviously I don't want every range to be named the same.
Any help is greatly appreciated!

Code:
Sub CreateRanges()
    Dim i As Integer, j As Integer, y As Integer, x As Integer
    Dim NewRange As Range
        i = 3: j = 1
            Do While Cells(1, j) <> vbNullString
                j = j + 1
            Loop
            For y = 1 To j
                Do While Cells(i, y) <> vbNullString
                    i = i + 1
                Loop
                Set NewRange = ActiveSheet.Range(Cells(3, y), Cells(i - 1, y))
                ActiveWorkbook.Names.Add Name:=Cells(1, y), RefersTo:=NewRange      'this line is highlighted when I click Debug
                y = y + 1
            Next
End Sub
 
Last edited:

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
try this
Code:
ActiveWorkbook.Names.Add Name:=Cells(1, y).Value, RefersTo:=NewRange.Address
 
Upvote 0
I still get the same error. I can change Cells(1,y) to Range("A1") and it works, but again I get only one new range created instead of all of the desired ranges in the worksheet.
 
Upvote 0
In case anyone views this thread here is the working code. The worksheet has ranges in each column and the first row is the range name and the second row is a blank space. All the rest of the entries follow underneath the empty cell. It then assigns the range name in the first row to the values underneath...my variables were incorrectly placed. Thanks.

Code:
Sub CreateRanges()
    Dim i As Integer, j As Integer, y As Integer, x As Integer
    Dim NewRange As Range
    Dim RangeName As String
        j = 1
            Do While Cells(1, j) <> vbNullString
                j = j + 1
            Loop
            j = j - 1
            For y = 1 To j
                i = 3
                    Do While Cells(i, y) <> vbNullString
                        i = i + 1
                    Loop
                i = i - 1
                Set NewRange = ActiveSheet.Range(Cells(2, y), Cells(i, y))
                RangeName = Cells(1, y)
                ActiveWorkbook.Names.Add Name:=Cells(1, y), RefersTo:=NewRange
            Next
End Sub
 
Upvote 0
Try
Code:
RefersTo: ActiveWorkbook.Names.Add Name:=Cells(1, y), RefersTo:= "=" & NewRange.Address(,,,True)
 
Upvote 0

Forum statistics

Threads
1,214,614
Messages
6,120,520
Members
448,968
Latest member
Ajax40

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