I want to embed a procedure in an Excel sheet that will detect when a cell's format changes, e.g. from Text to Number.
But I can't figure out how to get the cell's format type. I tried using the Worksheet_Change
event handler to check the data type, as follows:
Private Sub worksheet_change(ByVal Target As Range)
If Target.Address = "a1" Then
If VarType(Target) <> 5 Then
MsgBox "cell format has been changed"
End If
End If
End Sub
But with this code in place, if I change cell A1's data type from Number to Text, Worksheet_Change
is not triggered; the event handler is only called if I change the contents of the cell.
Also, this procedure can detect if the contents are changed from a number to an alphabetical string, e.g. from "35.12" to "abcd", but not Number-type number to Text-type number; if I set cell B1 to text, then enter "40", then paste the contents of cell B1 into cell A1, vartype()
still returns "5", so the alert is not triggered.
How can I detect that the format has changed, regardless of whether the content type has changed?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…