VBA Code to Name Range

OilEconomist

Active Member
Joined
Dec 26, 2016
Messages
439
Office Version
  1. 2019
Platform
  1. Windows
Hello and thanks in advance for your help and I will provide feedback.

Cells A4 through A28 have the name I would like to use as NameRanges for cells in Cells C4 through C28.

"I am getting a run-time error '1004': The name that you entered is not valid." on the following line:

Code:
Worksheets("1").Names.Add Name:=RangeNameString1, RefersTo:=NameRange1

Entire Code:

Code:
Sub RangeNameOneCell()


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


    
    Worksheets("1").Activate


    For i = 4 To 28


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




End Sub

Also same error on the following code:

Code:
Sub RangeName2()
    Dim i As Integer
     
    Dim RangeNameString1 As String
    Dim RangeNameString2 As String


    Dim NameRange1 As Range
    
    Worksheets("1").Activate


    For i = 35 To 41


            If ActiveSheet.Cells(i, 4).Value <> "" Then
    
            RangeNameString1 = ActiveSheet.Cells(i, 4).Value


            Set NameRange1 = ActiveSheet.Range(Cells(i, 7), Cells(i, 1207))
                
            Worksheets("1").Names.Add Name:=RangeNameString1, RefersTo:=NameRange1
            
                
         End If
         
         Next i


End Sub
 
When the error occurs press debug, in the code put the mouse pointer to the variable RangeNameString1 and check the contents of the variable.
 
Upvote 0

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December

Forum statistics

Threads
1,223,911
Messages
6,175,334
Members
452,636
Latest member
laura12345

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