VBA Code Run Time Error on Double Loop To Name Range

OilEconomist

Active Member
Joined
Dec 26, 2016
Messages
421
Office Version
  1. 2019
Platform
  1. Windows
Thanks in advance for any suggestions for which I will provide feedback.

I am trying to name range cells which start on row 6 and end on 51 between columns D (column 4) and Z (column 26).

The names are a combination of what's in row 6 through 51 in column A and columns D through X in row 1.

Why am I getting "Run-time error '1004' Application-defined or object-defined error" on the following line of code:
Code:
  Set NameRange1 = ActiveSheet.Range(i, j)


Entire Code:
Code:
Sub RangeNameOneCell()


    Dim i As Integer
    Dim j As Integer
    Dim RangeNameString1 As String
    Dim NameRange1 As Range


    
    Worksheets("1").Activate


    For i = 6 To 51
    
        For j = 4 To 26


            If ActiveSheet.Cells(i, 1).Value <> "" Then
    
            RangeNameString1 = ActiveSheet.Cells(i, 1).Value & "." & ActiveSheet.Cells(1, j).Value
            
            Set NameRange1 = ActiveSheet.Range(i, j)
                
            Worksheets("1").Names.Add Name:=RangeNameString1, RefersTo:=NameRange1
                
         End If
         
         Next j
         
    Next i




End Sub
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Apologies. I just figured it out after hours of messing with it. I cannot use range in that manner. When I change to Cells, it works.

Basically change

Code:
Set NameRange1 = ActiveSheet.Range(i, j)

to
Code:
Set NameRange1 = ActiveSheet.Cells(i, j)
 
Upvote 0

Forum statistics

Threads
1,214,944
Messages
6,122,391
Members
449,080
Latest member
Armadillos

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