Solved

Excel Name goes away when set to empty string

Posted on 2016-08-18
10
58 Views
Last Modified: 2016-08-19
I have an Excel 2010 workbook with Names.  When I set a Name to the empty string it disappears.  Is this known behavior?  Is there a way to keep a name around with an empty string value or do I have to put some sort of dummy value in there?
0
Comment
Question by:koughdur
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
10 Comments
 
LVL 48

Expert Comment

by:Martin Liss
ID: 41761598
Can you explain further what you mean when you say you "set a Name to the empty string"?
0
 

Author Comment

by:koughdur
ID: 41761605
ThisWorkbook.Names("MyName").Value = ""
0
 
LVL 48

Expert Comment

by:Martin Liss
ID: 41761611
Ok, when a Name has to be attached to something so it makes sense that it would go away when it isn't. I don't know why you'd want to keep it around (can you explain?), but certainly assigning something to it would keep it.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:koughdur
ID: 41761616
I want to use Names rather than Globals to save values.  A blank string is a valid value that I would want to save.  Mostly I associate names with cells, but these values are not really associated with any particular worksheet or cell.
0
 
LVL 48

Expert Comment

by:Martin Liss
ID: 41761630
Ok, this is a workaround. Run the FindOut sub and it will tell you if the name exists. If it doesn't you can treat it like it's blank.

Function NameExists(nme As String) As Boolean
Dim n As Name

For Each n In Names
    If nme = n.Name Then
        NameExists = True
        Exit Function
    End If
Next

End Function
Sub FindOut()
If Not NameExists("MyName1") Then
    MsgBox "It's gone"
End If
End Sub

Open in new window

0
 

Author Comment

by:koughdur
ID: 41761638
Thanks for the idea, but that will be rather cumbersome.  I'm using the Names to store previous values for a bunch of different cells.

When I was using globals I could just remember the values like this:

PrevValue = Range("MyCell").Value

and reset the cell later with the previous value like so:

Range("MyCell").Value = PrevValue

The problem with globals is that they go away if any of the macros error out.  Which means the user has to close and reopen the workbook in order for the globals to be recreated.  I was hoping that Names would stick around, but evidently Bill Gates is too smart for me and wants to save me space by getting rid of a perfectly good name just because it has an empty string in it.

What I'm going to do is find an obscure location on an obscure worksheet and store my Names associated with those cells then I can do this:

Worksheet("Obscure").Range("PrevValue") = Worksheet("Main").Range("MyCell").Value

and

Worksheet("Main").Range("MyCell").Value = Worksheet("Obscure").range("PrevValue")
0
 
LVL 48

Accepted Solution

by:
Martin Liss earned 500 total points
ID: 41761691
What about using custom document properties?

Sub SetUpVars()

With ActiveWorkbook.CustomDocumentProperties
    .Add Name:="Var1", _
        LinkToContent:=False, _
        Type:=msoPropertyTypeString, _
        Value:="save me"
    .Add Name:="Var2", _
        LinkToContent:=False, _
        Type:=msoPropertyTypeNumber, _
        Value:=1000
End With

End Sub

Sub CreateError()
MsgBox 6 / 0

End Sub
Sub ShowThem()

MsgBox ActiveWorkbook.CustomDocumentProperties("Var1")
MsgBox ActiveWorkbook.CustomDocumentProperties("Var2")
End Sub

Open in new window

0
 
LVL 47

Expert Comment

by:Wayne Taylor (webtubbs)
ID: 41761763
...evidently Bill Gates is too smart for me and wants to save me space by getting rid of a perfectly good name just because it has an empty string in it.

This line of code...

    ThisWorkbook.Names("MyName").Value = "" 

...is not setting the Name value to an empty string. It is in fact the equivalent to the RefersTo property in that it refers to the formula (see here). So, while this...

   ThisWorkbook.Names("MyName").Value = "bob"

...looks like it sets the value to "bob", it actually sets the value to ="bob".

With this in mind, instead of using the Value property, how about using the Comment value? That can actually be an empty string and will be retained.
0
 

Author Closing Comment

by:koughdur
ID: 41762740
Martin:  Thanks.  I had forgotten about those.  I will look to see if I can implement, but I may go with Named cell ranges as that is what I am using for saving other values.

Wayne:  I am saving some stuff in comments, but there are issues with those as well.  They are a bit kludgy to get things in and out of and the show up as red triangles in the cells.  I'm actually thinking of moving away from those using Named cell ranges for those as well.
0
 
LVL 48

Expert Comment

by:Martin Liss
ID: 41762762
You're welcome and I'm glad I was able to help.

In my profile you'll find links to some articles I've written that may interest you.

Marty - Microsoft MVP 2009 to 2016
              Experts Exchange MVE 2015
              Experts Exchange Top Expert Visual Basic Classic 2012 to 2015
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question