Search code examples
excelvbanamed-ranges

Getting the value of a Name with no range


I have a workbook, UserFileBook, that contains the Name 'Version' which simply refers to a number (it does not refer to any range, in the Name Manager it just 'Refers to =5'). I am trying to compare this number with the version number of a different workbook. When I had UserFileBook's 'Version' as an actual named range (it referred to cell C1 which had the value of 5 in it) everything worked fine. But IdiotUser can edit that value or delete it right on the sheet, so I made it just refer to a number so it can only be edited through the manager. Is there a way for me to obtain the value of that Name and alter it from another WB now? Currently I'm trying this:

Sub CheckVersionNumber(Vers As Long)

'Checks to see if this version is compatible with the UW version
Dim wb As Workbook
Set wb = UserFileBook

Dim UWVers As Long
UWVers = wb.Names("Version").Value 'Breaks here

'Version information is in the range "Version" on UW
If UWVers < Vers Then
    GoTo LowerVersion
    
Else
    If wb.Names("Version") > Vers Then  'tried this originally and also breaks, also if .Value is added
        GoTo UpperVersion
    End If
End If

Exit Sub

I also tried comparing to wb.Range("Version"), and even wb.Worksheets("Sheet 1").Range("Version) but those didnt work either. How can I reference (and alter) the value of "Version" in the USerFileBook if it doesn't refer to a range?


Solution

  • You cannot use .Range because Version is not a range. It's a named formula.

    But you can evaluate it:

    UWVers = wb.Worksheets(1).Evaluate("Version")
    

    To update the named formula with a different value, say 999:

    wb.Names.Add "Version", 999
    

    To make the named formula invisible in the Name Manager:

    wb.Names.Add "Version", 999, True
    

    As an aside... since you are having difficulties with users changing your solution settings you may wish to explore utilizing CustomXMLParts.Add to store your Version. There is no user interface to CustomXMLParts, but they are stored in the workbook. The only way to access them is through code. A normal user will NEVER see your version number stored this way. In fact most advanced developers would never find it either.