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

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"

Forum statistics

Threads
1,224,828
Messages
6,181,213
Members
453,024
Latest member
Wingit77

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