我遇到了这个确切的问题,并根据需要找到了几个可能的解决方案。
将数据保存在Hyperlink
您可以将数据存储在超链接中,例如链接本身或链接的工具提示中。这种方法的优点是数据与单元本身相关联,而不是与地址相关联。这意味着如果有人对单元格进行排序,则隐藏数据会随之而来。如果您愿意,您也可以捕获 HyperlinkClick 事件,但是这些超链接都没有到达任何地方,因此它可能无关紧要。
使用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
使用Address
这可能对您可以保存的字符串类型有一些限制,因为它们必须是有效的链接。例如,其中不能有任何空格,但您当然可以使用代码(如\20
)来表示空格并稍后对其进行解码。
免责声明:我实际上并不完全确定这是做什么的,我试图找到一种方法来创建一个没有去任何地方的有效链接,这就是我想出的。它有点破坏 excel,因为您不能再使用 Excel GUI 编辑超链接,并且单击它不会触发FollowHyperlink
事件。如果您只是设置?Data
为地址,那么 Excel 将在下次有人单击它时将其清除。
' 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
将数据保存在Validation
也可以在单元格的验证中存储数据。但是,如果有人对细胞进行分类,这将不起作用。
' 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