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

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
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

I am still get 1004 error with this update based off your code:

Code:
Option Explicit


Sub NameRanges()
    Dim ws As Worksheet
    Dim cl As Range
    Dim wsIndex As Integer
    Dim RngInput As Object
    Dim RngCalc As Object




    For Each ws In ThisWorkbook.Worksheets


        For Each cl In ws.UsedRange
            If cl.Interior.Color = vbYellow Then
                If RngInput Is Nothing Then Set RngInput = cl Else Set RngInput = Union(RngInput, cl)
            ElseIf cl.HasFormula() = True Then
                If RngCalc Is Nothing Then Set RngCalc = cl Else Set RngCalc = Union(RngCalc, cl)
            End If
        Next cl
        
        If ws.Index <= 8 Then
            wsIndex = ws.Index
        Else
            wsIndex = ws.Index - 8 & "2"
        End If
    
        MsgBox "test1"
        ThisWorkbook.Names.Add _
                    Name:="InputsWS" & wsIndex, _
                    RefersToR1C1:="'" & ws.Name & "'!" & RngInput.Address(1, 1, xlR1C1)
        MsgBox "test2"
        ThisWorkbook.Names.Add _
                    Name:="Calc" & wsIndex, _
                    RefersToR1C1:="'" & ws.Name & "'!" & RngCalc.Address(1, 1, xlR1C1)
        
        Set RngInput = Nothing
        Set RngCalc = Nothing
    Next ws
End Sub
 
Upvote 0
Your addition of the equal sign with the range application fixed the error. Unfortunately it didnt name all the correct cells so I need to figure that out lol!
 
Last edited:
Upvote 0
Actually it looks to be right. I would think the name property would change the top left name when you click on the cell. Doesnt seem to be doing that but it shows up in the name manager.
 
Upvote 0
Glad it's sorted & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,912
Messages
6,175,340
Members
452,638
Latest member
Oluwabukunmi

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