1

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:

  1. Cells(2, i).Address is the cell id for the second row (Eg: $A$2)
  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 DESREFwithout adding the Sheet1!

How can I prevent the Sheet1! from appearing at the beginning?
Thank you

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • When you say the VBA is changing it to `=Sheet1!DESREF(Sheet1!$A$2,0,0,CONTARA(Sheet1!$A:$A)-1)`, what specific VBA is doing this? It looks like your initial snippet would overwrite whatever is there. Can you point to the specific line of VBA that results in this change? – Hambone Feb 15 '16 at 03:03
  • It's in the line: `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)"` Also, even when I try to replace it with `nms(i).RefersTo = Replace(nms(i).RefersTo, ActiveSheet.Name & "!DESREF", "DESREF")`it still writes _Sheet1!DESREF_. On another hand, If I do `nms(i).RefersTo = Replace(nms(i).RefersTo, ActiveSheet.Name & "!DESREF", "DESREFF")` it will write `=DESREFF(Sheet1!$A$2,0,0,CONTARA(Sheet1!$A:$A)-1)` – Antonio Nó Rodríguez Feb 15 '16 at 08:33
  • Wow, this is crazy. I feel like I could get this working, if only I could repeat the issue. Every time, Excel/VBA does exactly what I think it should -- I cannot get it to recreate your error. If this is still an issue and you can repeat the steps (in your OP) that cause the error, I'll be happy to take a look at it. You might have to be specific -- "Highlight column C, name it "blah", run VBA Sub 'XYZ'." I realize it may not be that important to you, so I'll let you decide. – Hambone Feb 15 '16 at 12:39
  • I'm going to try this on another Excel version in case it's a bug specific on the version 14.3.1 of Mac. I'll let you know about the results. – Antonio Nó Rodríguez Feb 15 '16 at 12:50
  • Ok, it seems to be a bug in the version. If I do it in Excel for Mac 14.3.9 it doesn't add the _Sheet!_ at the beginning, but doing `=SUMA(name)` doesn't work until I go to the edition of names and press enter in the name. This also happens on _Excel for Windows 16.0.6001.1054_ You can create names with VBA but to use them you must edit the name later and hit enter. Does this happen to you? – Antonio Nó Rodríguez Feb 15 '16 at 14:01
  • 1
    It's sort of quirky... I just tried it, and I *Can* use the names, both using F5 and with the box in the upper left, but they don't display as options in the drop-down or show up as such when I select them... in other words, it *sort of* works. Maybe there is a "register" event of some type that enables other parts of Excel to let it work the same way as the UI? – Hambone Feb 15 '16 at 14:07
  • Yes, it seems strange because this happens with all versions of Excel. [In the manual](https://msdn.microsoft.com/en-us/library/office/ff835300.aspx) they give an example and they don't do anything after assigning the name. I will check at night. – Antonio Nó Rodríguez Feb 15 '16 at 14:22
  • Good luck... it's a heck of a puzzle. I wish I could upvote it twice. – Hambone Feb 15 '16 at 14:36

1 Answers1

0

I've tried with different versions of Excel and it seems to be a bug in Excel 2011 for Mac (version 14.3.1).

I tried it in the following versions and it didn't add the Sheet1! at the beginning:

  1. Excel for Mac 14.3.9
  2. Excel for Windows 16.0.6001.1054

The problem now with both of those versions is the following:
I have a column with the following cells:

C1-> data
C2-> 400
C3-> 100
C4-> 100

And after C5 empty. I run the script to create the names:

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

And it created me the name Sheet1data with the reference: =DESREF(Sheet1!$C$2;0;0;CONTARA(Sheet1!$C:$C)-1) which is correct. But if I go to any cell and insert the formula =SUM(Sheet1data) it resolves with a formula error #Name?, even though explaining the formula seems to point to the right cells.

More strange, if after creating it I go to edit the name and just hit Enter, the formula works automatically and presents 600. If I go again to the name editor it shows =DESREF(Sheet1!$C$2;0;0;CONTARA(Sheet1!$C:$C)-1), which is the same as before.

Finally I found this web with examples where there was one explaining something similar to what I wanted to do:

Sub DynamicNames()
  Dim LastCol As Long, _
      LabelRow As Long, _
      Col As Long
  Dim sName As String
  Dim c As Range
  Dim Sht As String

  'assign row and column parameters
  '**adjust for the row containing your headings
  LabelRow = 1
  LastCol = Range("IV1").End(xlToLeft).Column

  'grab sheet name
  Sht = "'" & ActiveSheet.Name & "'"

  For Each c In Range(Cells(LabelRow, 1), Cells(LabelRow, LastCol))
    Col = c.Column
    sName = c.Value
    If Len(sName) > 1 Then
      'replace spaces with underscores
      sName = Replace(sName, " ", "_", 1) 
      'create the name
      ActiveWorkbook.Names.Add Name:=sName, RefersToR1C1:= _
        "=OFFSET(" & Sht & "!R2C" & Col & ",0,0,COUNTA(" & Sht & "!C" & Col & ")-1,1)"
    End If
  Next c
End Sub

The main difference is that they where using RefersToR1C1 instead of RefersTo, so I changed it but it still wasn't working. Finally I changed the formulas to english even though I had to write them in spanish by hand and that did the final trick: Names.Add Name:=ActiveSheet.Name & Range(Cells(1, i).Address).Value, RefersToR1C1:="=OFFSET(" & ActiveSheet.Name & "!R2C" & Cells(2, i).Column & ",0,0,COUNTA(" & ActiveSheet.Name & "!C" & Cells(2, i).Column & ")-1)"

So, the problems where:

  1. The Excel 2011 for Mac (version 14.3.1) seems to have a bug.
  2. Using RefersToR1C1 instead of RefersTo makes it easier to work with vba formulas and maybe it helped.
  3. Even though in all my Excel I use spanish formulas, the ones in vba have to be in english. (I tried the parameter _ RefersToR1C1Local_ but it gave me an error).

Finally, the code that worked:

Sub Botón6_AlHacerClic()
    For i = 1 To Columns.Count
        Names.Add Name:=ActiveSheet.Name & Range(Cells(1, i).Address).Value, RefersToR1C1:="=OFFSET(" & ActiveSheet.Name & "!R2C" & Cells(2, i).Column & ",0,0,COUNTA(" & ActiveSheet.Name & "!C" & Cells(2, i).Column & ")-1)"
    Next i
End Sub

I hope it helps someone out there :)