I'm working with some data on Excel 2011 for Mac (version 14.3.1) that I need to graph and add more data later on. To do that, I'm using names for ranges as it is explained here. Basically, create Name ranges with the formula =OFFSET($A$2,0,0,COUNTA($A:$A)-1)
(in spanish: =DESREF($A$2,0,0,CONTARA($A:$A)-1)
).
I have many columns, so I wrote this vba to do it for me:
Sub Botón6_AlHacerClic()
For i = 1 To Columns.Count
Names.Add Name:=ActiveSheet.Name & Range(Cells(1, i).Address).Value, RefersTo:="=DESREF(" & Cells(2, i).Address & ",0,0,CONTARA(" & Replace(Cells(1, i).Address, "$1", "") & ":" & Replace(Cells(1, i).Address, "$1", "") & ")-1)"
Next i
End Sub
Where:
Cells(2, i).Address
is the cell id for the second row (Eg: $A$2)Replace(Cells(1, i).Address, "$1", "")
is the row letter (Eg: $A)
The problem I'm having is that when entering the names manually, it replaces the formula =DESREF($A$2,0,0,CONTARA($A:$A)-1)
with =DESREF(Sheet1!$A$2,0,0,CONTARA(Sheet1!$A:$A)-1)
which is fine and works great. But when I do it by the vba, it replaces it with =Sheet1!DESREF(Sheet1!$A$2,0,0,CONTARA(Sheet1!$A:$A)-1)
that doesn't work.
I tried with the different options of the Add manual and even tried to run this code after the names are created to eliminate the Sheet1! at the beginning but at the end they keep the Sheet1!:
Sub Botón7_AlHacerClic()
Set nms = ActiveWorkbook.Names
For i = 1 To nms.Count
nms(i).RefersTo = Replace(nms(i).RefersTo, ActiveSheet.Name & "!DESREF", "DESREF")
Next i
End Sub
Another thing I tried was to replace the Sheet1!DESREF for something that is not a function:
Sub Botón7_AlHacerClic()
Set nms = ActiveWorkbook.Names
For i = 1 To nms.Count
nms(i).RefersTo = Replace(nms(i).RefersTo, ActiveSheet.Name & "!DESREF", "DESREFF")
Next i
End Sub
And in this case it gives me: =DESREFF($A$2,0,0,CONTARA($A:$A)-1)
But I haven't find a way to do it with the DESREF
without adding the Sheet1!
How can I prevent the Sheet1! from appearing at the beginning?
Thank you