How to store an extra piece of (hidden) information with a cell in excel
Asked Answered
C

6

7

I'm writing an application for work which compiles a whole mess of different spreadsheets into reports for a specific work site. I have quite a few worksheets with a whole bunch of metrics on each. Each metric on each sheet might need to be compiled from a different worksheet and would be found on that worksheet by looking up a keyword.

Another problem is that some of the wording on these spreadsheets gets changed periodically to better reflect industry standards so I can't just hardwire in keywords to search for.

So I'm looking for a way to store metadata along with a cell that I can hide from users so they don't accidentally delete it but can access easily from VBA in order to change if needed (I would write a procedure to do this if needed).

Research said I could use comments (though I'm not sure if I can access these from vba, and I would like them to be hidden) or a hidden worksheet to mirror each of the worksheets I use with information in the given cell on that worksheet. I would probably go with the latter but it's not perfect.

Any other ideas?

Edit for clarity: I need a string associated with a single cell which will point me at how to find the appropriate data. Ex: "Look in Workbook 1 -%- Search for this text".

The VeryHidden Attribute mentioned by @Andrew might be the best way to go as I don't think that there is a way to attach a variable to a cell.

Chigoe answered 5/7, 2012 at 22:3 Comment(8)
I am not sure if I understand your actual requirement. Can you give me an example?Archicarp
Either comments or the hidden sheet would work (though like Siddharth I'm not exactly clear on the actual requirements). Depending on how many pieces of information you needc to keep you could also use Names or Custom document Properties (vbaexpress.com/kb/getarticle.php?kb_id=677).Comet
Hidden worksheets are an option, especially if you set the visibility to VeryHidden so they can only be unhidden with VBA. Could you get away with one, with different metadata in different ranges on that sheet?Cresol
@SiddharthRout To be clearer, I just want a string of text associated with a couple of cells on each worksheet which I can process to find out a couple of properties on where to find the appropriate data. Will edit my OP. I think Andrew's post about using VeryHidden may be the way to go.Chigoe
@Colophi: Hidden worksheets are very tedious to maintain if you have lot of sheets in your workbook. You can get away with 1 worksheet if you do not have to Mirror each worksheet to this worksheet like you mentioned above. As for the comments, A comment can be handled from vba but then those can be deleted from the worksheet if the worksheet is not protected. If it is couple of cells from each sheet then using 1 hidden worksheet will definitely help.Archicarp
When you say "a couple of cells" do you mean one? a range? or multiple single cells? (your question says single, but comment says couple)Flowerlike
@SiddharthRout It's 7 sheets and I don't think that will change, so I don't think that'll be too bad. However, I'm thinking now that I may just use one hidden worksheet instead of 7. I'll just come up with some sort of a structure to represent each worksheet. In total I only need to store information for 35-45 cells so I think that's best.Chigoe
If you were able to attach a variable to a cell how would you access the data? Are you trying to attach a keyword (or keywords) to a cell and be able to lookup that same key using the keyword?Flowerlike
L
10

One of my favourite tricks is to use the N() function.

N() converts a non number to a number but if you feed it text it always returns 0. When I need to add an in cell note to myself I'll add something like:

=..... +N("This is to calculate blah blah blah")

As long as adding 0 to the value won't hurt it works well.

Luthern answered 6/7, 2012 at 0:43 Comment(1)
Seems like this doesn't work for other types of cells (text, currency, dates), also the user can see these comments when they select the cell. This would seem like a quick and dirty method though.Amar
C
4

What I have done in the past when needing to store data for the application but which should not be accessible by users is to use a sheet with visibility set to VeryHidden.

There may also be the option of creating an Excel Add-in (XLA) which can be independent of user data and so can be updated separately if rules need to change — although it is possible to use VBA in an "update" workbook to replace entire VBA modules in the "data" workbook. I'll have to see if I can find the code I wrote to do that.

Cresol answered 5/7, 2012 at 22:58 Comment(1)
Although this does takes some work, it does work very well at the end if you mirror the worksheet. Thanks.Amar
H
2

You can use a similar trick for Text.

You can use:

="Display Value" & left("This is to calculate...",0)

OR:

=CHOOSE(1,"Display Value","This is to calculate...")
Hypocotyl answered 24/7, 2017 at 6:0 Comment(0)
C
1

I encountered this exact problem and found a couple of possible solutions depending on need.

Save data in Hyperlink

You can store data in hyperlinks for example in the link itself or in the tooltip for the link. The advantage of this method is that the data is tied to the cell itself, and not to the address. Meaning that if e.g. someone sorts the cells the hidden data follows. You could also catch the HyperlinkClick event if you wished, but none of these hyperlinks go anywhere, so it probably doesn't matter.

Using ScreenTip

' Write hidden data to a cell
Public Sub WriteData(ByVal Cell As Range, ByVal Data As String)

    Cell.Hyperlinks.Delete
    Cell.Hyperlinks.Add Cell, Address:="#", ScreenTip:=Data

    ' Set standard formatting
    With Cell.Font
        .Underline = xlUnderlineStyleNone
        .ColorIndex = xlAutomatic
    End With

End Sub

' Read hidden data from a cell
Public Function ReadData(ByVal Cell As Range) As String

    If Not CellContainsData(Cell) Then Exit Function

    ReadData = Cell.Hyperlinks(1).ScreenTip

End Function

Private Function CellContainsData(ByVal Cell As Range) As Boolean

    CellContainsData = Cell.Hyperlinks.Count > 0

End Function

Using Address

This probably has some restrictions on the kind of string you can save as they have to somewhat be valid links. For example it cannot have any spaces in them, but you could of course use a code (like \20) to represent a space and decode it later.

Disclaimer: I am actually not entirely sure what this does, I was trying to find a way to create a valid link that didn't go anywhere and this is what I came up with. It somewhat breaks excel as you can no longer edit the hyperlink with the Excel GUI, and clicking it doesn't trigger a FollowHyperlink event. If you simply set ?Data as the address then Excel will clear it the next time anyone clicks it.

' Write hidden data to a cell
Public Sub WriteData(ByVal Cell As Range, ByVal Data As String)

    Cell.Hyperlinks.Delete
    Cell.Hyperlinks.Add Cell, Address:="//?" & Data, ScreenTip:="Nothing suspicious here."

    ' Set standard formatting
    With Cell.Font
        .Underline = xlUnderlineStyleNone
        .ColorIndex = xlAutomatic
    End With

End Sub

' Read hidden data from a cell
Public Function ReadData(ByVal Cell As Range) As String

    If Not CellContainsData(Cell) Then Exit Function

    Dim Data As String
    Data = Cell.Hyperlinks(1).Address    

    ReadData = Right$(Data, Len(Data) - 3)

End Function

Private Function CellContainsData(ByVal Cell As Range) As Boolean

    If Cell.Hyperlinks.Count < 1 Then Exit Function

    Dim Data As String
    Data = Cell.Hyperlinks(1).Address

    ' Check that the cell has the correct prefix
    If Len$(Data) < 3 Then Exit Function
    If Left$(Data, 3) <> "\\?" Then Exit Function

    CellContainsData = True

End Function

Save data in Validation

It is also possible to store data in the validation of a cell. This however does not work if e.g. someone sorts the cells.

' Write hidden data to a cell
Public Sub WriteData(ByVal Cell As Range, ByVal Data As String)

    With Cell.Validation
        ' Remove previous validation
        .Delete

        ' Write data on a specific format so you know it was you.
        .Add xlValidateCustom, Formula1:="""@" & Data & """"

        ' Hide it as well as possible
        .InCellDropdown = False
        .ShowInput = False
        .ShowError = False
    End With

End Sub

' Read hidden data from a cell
Public Function ReadData(ByVal Cell As Range) As String

    If Not CellContainsData(Cell) Then Exit Function

    Dim Data As String
    Data = Cell.Validation.Formula1

    ReadData = Mid$(Data, 3, Len(Data) - 3)

End Function

Private Function CellContainsData(ByVal Cell As Range) As Boolean

    On Error GoTo InvalidValidation

    If Cell.Validation.Type <> xlValidateCustom Then Exit Function

    Dim Data As String
    Data = Cell.Validation.Formula1

    ' Check that the data is on your specific format
    If Left$(Data, 2) <> """@" Then Exit Function
    If Right$(Data, 1) <> """" Then Exit Function

    CellContainsData = True

InvalidValidation:
    Exit Function

End Function
Choice answered 3/10, 2019 at 15:22 Comment(1)
Quite informative. TY!Crackerjack
S
1

This is a very interesting question. Thanks to Colophi! What about using the ID of the cell?

In the style of Jonas Glesaaen:

Public Sub WriteHiddenData(ByVal Cell As Range, ByVal Data As String)

    Cell.ID = """@" & Data & """"

End Sub

Public Function ReadHiddenData(ByVal Cell As Range) As String

    Dim Data As String
    Data = Cell.ID

    ' Check that the data is on your specific format
    Dim L%
    L = Len(Data)
    If L < 3 Then Exit Function
    If Left$(Data, 2) <> """@" Then Exit Function
    If Right$(Data, 1) <> """" Then Exit Function

    ReadHiddenData = Mid$(Data, 3, L - 3)

End Function
Shortridge answered 3/7, 2021 at 6:10 Comment(0)
E
0

Every spreadsheet should have labels or headers, or at least some description fields. If this is true, there is a trick that you can hide a value in one of those cells and nobody will find out. Here is how you do it.

  1. Enter the value in the cell with =N(). For example:

=N("Apple, Google, Facebook, Microsoft").

  1. Go to Format Cells > Custom: Enter the text you want to display for the header/label/description. For example:

"Header Name".

As you can see the attached image that I was able to enter text in the cell but shows something different. And as a bonus, you can use this syntax below to get the formula/text back from VBA for your program:

Range("A1").Formula

Hope this helps.

enter image description here

Emsmus answered 31/8, 2017 at 17:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.