I have a number of named ranges within the scope of my Workbook
.
Now I want to loop over each named range by following the second answer of this related topic
Sub Range1()
Dim i As Integer
For i = 2 To ActiveWorkbook.Names.Count
MsgBox ActiveWorkbook.Names(i).Name
Next i
End Sub
This macro shows me all the names such as C_1
in a msgbox. I modify the script in the following way to "call" each of the ranges based on the name of the corresponding range.
Sub Range2()
Dim i As Integer
Dim rng As Range
Dim nm As Name
Dim rng_name As String
For i = 2 To ActiveWorkbook.Names.Count
Set nm = ActiveWorkbook.Names(i)
rng_name = nm.Name
Set rng = Application.Range(nm.Name) `This line yields an error
MsgBox rng.Address
Next i
End Sub
The error I get is:
"Run-time error '1004': Method 'Range' of object'_ Apolication failed
Any idea what I am doing wrong? If I inspect the elements I see that rng_name
is equal to "C_1".
EDIT
I believe that it has something to do with how the named ranges are created. This is done in the following way:
Sub createRanges()
Dim LastRowAll As Long, LastRowUnique As Long
Dim x, y
Dim rng As Range
Dim rng_name As String
LastRowUnique = Sheets("Lists").Range("J2").End(xlDown).Row
LastRowAll = Sheets("Deribit").Range("D8").End(xlDown).Row
For Each x In Sheets("Lists").Range("J2:J" & LastRowUnique)
For Each y In Sheets("Deribit").Range("D8:D" & LastRowAll)
If y.Offset(0, -1).value = "Call" Then
If rng Is Nothing And y = x Then
Set rng = y.Offset(0, -2)
ElseIf y = x Then
Set rng = Union(rng, y.Offset(0, -2))
End If
Else:
End If
Next y
rng_name = "C_" & x.Offset(0, -1).value
ThisWorkbook.Names.Add Name:=rng_name, RefersTo:=rng.Address
Set rng = Nothing
Next x