Error 1004 for string name in refersto

bradyboyy88

Well-known Member
Joined
Feb 25, 2015
Messages
562
My code is drawing a 1004 application defined error and I have found it to be because of the refersto. What is going wrong with this?

Code:
Option Explicit


Sub NameRanges()


Dim ws As Worksheet
Dim cl As Range
Dim wsIndex As Integer
Dim InputString As String
Dim CalcString As String


InputString = "= "
CalcString = "= "


    For Each ws In ThisWorkbook.Worksheets
        For Each cl In ws.UsedRange
            If cl.Interior.Color = vbYellow Then
                InputString = InputString & "'" & ws.Name & "'!" & cl.Address(ReferenceStyle:=xlR1C1) & ", "
            ElseIf cl.HasFormula() = True Then
                CalcString = CalcString & "'" & ws.Name & "'!" & cl.Address(ReferenceStyle:=xlR1C1) & ", "
            End If
        Next cl
        
        If ws.Index <= 8 Then
            wsIndex = ws.Index
        Else
            wsIndex = ws.Index - 8 & "2"
        End If


        Debug.Print Left(InputString, Len(InputString) - 2)
        ThisWorkbook.Names.Add _
                Name:="InputsWS" & wsIndex, _
                RefersToR1C1:=Left(InputString, Len(InputString) - 2)


        ThisWorkbook.Names.Add _
                Name:="Calc" & wsIndex, _
                RefersToR1C1:=Left(CalcString, Len(CalcString) - 2)
    Next ws
End Sub
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
How long is the string when you get the error?
 
Upvote 0
Try this

Code:
Sub NameRanges()
    Dim ws As Worksheet
    Dim cl As Range
    Dim wsIndex As Integer
    Dim InputString As String
    Dim CalcString As String


    For Each ws In ThisWorkbook.Worksheets
        InputString = "= "
        CalcString = "= "
        For Each cl In ws.UsedRange
            If cl.Interior.Color = vbYellow Then
                InputString = InputString & "'" & ws.Name & "'!" & cl.Address(ReferenceStyle:=xlR1C1) & ", "
            ElseIf cl.HasFormula() = True Then
                CalcString = CalcString & "'" & ws.Name & "'!" & cl.Address(ReferenceStyle:=xlR1C1) & ", "
            End If
        Next cl
        
        If ws.Index <= 8 Then
            wsIndex = ws.Index
        Else
            wsIndex = ws.Index - 8 & "2"
        End If


        If InputString <> "= " Then
            Debug.Print Left(InputString, Len(InputString) - 2)
            ThisWorkbook.Names.Add _
                    Name:="InputsWS" & wsIndex, _
                    RefersToR1C1:=Left(InputString, Len(InputString) - 2)
        End If
        If CalcString <> "= " Then
            ThisWorkbook.Names.Add _
                    Name:="Calc" & wsIndex, _
                    RefersToR1C1:=Left(CalcString, Len(CalcString) - 2)
        End If
    Next ws
End Sub
 
Upvote 0
Try this

Code:
Sub NameRanges()
    Dim ws As Worksheet
    Dim cl As Range
    Dim wsIndex As Integer
    Dim InputString As String
    Dim CalcString As String


    For Each ws In ThisWorkbook.Worksheets
        InputString = "= "
        CalcString = "= "
        For Each cl In ws.UsedRange
            If cl.Interior.Color = vbYellow Then
                InputString = InputString & "'" & ws.Name & "'!" & cl.Address(ReferenceStyle:=xlR1C1) & ", "
            ElseIf cl.HasFormula() = True Then
                CalcString = CalcString & "'" & ws.Name & "'!" & cl.Address(ReferenceStyle:=xlR1C1) & ", "
            End If
        Next cl
        
        If ws.Index <= 8 Then
            wsIndex = ws.Index
        Else
            wsIndex = ws.Index - 8 & "2"
        End If


        If InputString <> "= " Then
            Debug.Print Left(InputString, Len(InputString) - 2)
            ThisWorkbook.Names.Add _
                    Name:="InputsWS" & wsIndex, _
                    RefersToR1C1:=Left(InputString, Len(InputString) - 2)
        End If
        If CalcString <> "= " Then
            ThisWorkbook.Names.Add _
                    Name:="Calc" & wsIndex, _
                    RefersToR1C1:=Left(CalcString, Len(CalcString) - 2)
        End If
    Next ws
End Sub

That did not work. Its almost guarenteed that there is a InputString and CalcString for all sheets.
 
Upvote 0
There are too many cells in the InputString, excel does not support it.
Instead of cledas you could put ranges of cells
 
Upvote 0
The limit is about 2100
Try
Code:
    For Each ws In ThisWorkbook.Worksheets
        For Each cl In ws.UsedRange
            If cl.Interior.Color = vbYellow Then
               If rng Is Nothing Then Set rng = cl Else Set rng = Union(rng, cl)
            ElseIf cl.HasFormula() = True Then
                CalcString = CalcString & "'" & ws.Name & "'!" & cl.Address(ReferenceStyle:=xlR1C1) & ", "
            End If
        Next cl
        
        If ws.Index <= 8 Then
            wsIndex = ws.Index
        Else
            wsIndex = ws.Index - 8 & "2"
        End If

        Debug.Print "'" & ws.Name & "'!" & rng.Address(1, 1, xlR1C1)

        ThisWorkbook.Names.Add _
                Name:="InputsWS" & wsIndex, _
                RefersToR1C1:="'" & ws.Name & "'!" & rng.Address(1, 1, xlR1C1)


        ThisWorkbook.Names.Add _
                Name:="Calc" & wsIndex, _
                RefersToR1C1:=Left(CalcString, Len(CalcString) - 2)
    Next ws
& similar for the Calc range
 
Last edited:
Upvote 0
The RefersTo argument should start with an equals sign.
Code:
ThisWorkbook.Names.Add _
                    Name:="InputsWS" & wsIndex, _
                    RefersToR1C1:= "=" & Left(InputString, Len(InputString) - 2)
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

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