Invalid Name when referencing the .Name property of a range.

grantboshart

New Member
Joined
Jul 1, 2015
Messages
49
Hi all,

I have a line of code that is generating the below response.

Code:
Range("F" & nrow, "F" & llr).Name = Header(iii)

Error:
<code style="margin: 0px; padding: 0px; border: 0px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, sans-serif; white-space: inherit;">**The name that you entered isnot valid
Reason
for this:
1) The name does not begin with a letter or underscore
2) The name contains a space or other invalid character
3) The name conflicts with a Excel built in name or name of another objectin the workbook**

</code>
*nrow* and *llr* are both integers and Header is a collection containing text strings. It is getting caught up beginning at the ".Name" statement

full code below but i do not believe it is necessary. To be clear, this code works perfectly in the beta excel sheet that i created it in.

Code:
Sub Create_Headers()

Application.EnableEvents = False




Dim rng As Range
Dim Dn As Range
Dim c, x, y As Long
Dim ray() As Variant
Dim Header As New Collection




Dim fr As Long
Dim lr As Long
Dim nrow As Long
Dim llr As Long


'_______________________________________________________________




'Workbooks("Named Ranges").Activate
Set rng = Worksheets("HGL").Range(Worksheets("HGL").Range("A10"), _
Worksheets("HGL").Range("A" & Rows.Count).End(xlUp))


'ReDim ray(1 To rng.Count)
ReDim ray(1)
c = 0




        For Each Dn In rng
            If Not Dn = vbNullString Then
                    c = c + 1
                    Debug.Print c & " " & Dn.Value
                    
                    ReDim ray(c)
                    ray(c) = Dn.Value
                    Debug.Print ray(c)
                    Header.Add ray(c)
                    Debug.Print Header.Count
                    Debug.Print Header(c)
                    
                    
            End If
        Next Dn
         
      
'Worksheets("Sheet1").Range("T1").Resize(c) = Application.Transpose(ray)
rayl = UBound(ray)
Debug.Print "ray has a size of " & rayl
For ii = 1 To rayl
    Debug.Print Header(ii)
Next ii


lr = Range("C" & Rows.Count).End(xlUp).Row
Debug.Print "The lower bound of the used range is row " & lr


'______________________________________________________________________
 


For iii = 1 To rayl


    Debug.Print "Header " & iii & ": which has a value of " & Header(iii)
    
    
    nrow = Columns(1).Find(what:=Header(iii)).Row
    Debug.Print Header(iii) & " can be found on row " & nrow
    For i = nrow To lr + 1
    
        Debug.Print Range("C" & i).Address
        
        If Range("C" & i).Value = vbNullString Then
            llr = i - 1
            Exit For
        End If
    Next i
    
    With Range("C" & nrow, "C" & llr)
        .Name = Header(iii)
    End With
    Debug.Print Header(iii) & " range is located at " & Range(Header(iii)).Address
    
    olength = Range(Header(iii)).Rows.Count
    
    Debug.Print olength
    
        
'    For i = 1 To olength
'        Debug.Print Range(Header(iii))(i).Value
'        Range("A" & i).Value = Range(Header(iii))(i).Value
'    Next i




Next iii


'this code below inputs the summary data into a specified range.
For i = 1 To rayl


    Debug.Print i & Header(i)
    Debug.Print Range("K" & i).Address
    Worksheets("Sheet1").Range("K" & i).Value = Header(i)
    Worksheets("Sheet1").Range("L" & i).Value = Application.WorksheetFunction.Sum(Range(Header(i)))
    
Next i


Application.EnableEvents = True










End Sub




when hovering over the .Name portion of the code in debug mode, the comment box that pops up says "<application defined or object defined error>"<application-defined or="" object-defined="" error="">


any idea why it is triggering this error? no other workbooks are open.

Thanks in advance!!!</application-defined>
 
Last edited:

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
edit to the above:

when hovering over the .Name portion of the code in debug mode, the comment box that pops up says "application defined or object defined error<application defined="" or="" object="" error="">"</application>
 
Upvote 0
**Update** the name that i am trying to store as the range name contains "-". This is what is causing it to trip up. How can i allow this to be part of the text string? example: "SD-A", "SD-B",...
 
Upvote 0

Forum statistics

Threads
1,221,310
Messages
6,159,176
Members
451,543
Latest member
cesymcox

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