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