与字符串比较时如何使用“TypeOf ___ is ___ Then”

How to use " TypeOf ___ is ___ Then " when comparing with a String

我想检查用户输入。如果我在 myRange 中有一个包含“2..4”的单元格,它通过了 IsNumeric 测试并为真,这就是我寻找其他选项的原因。

如果我运行一个包含

的函数
MsgBox (TypeName(myRange.Cells(i, j).Value))

我收到一条消息,指出 2..4 是 String 但对于以下各项,我的代码无法编译:

' expects Object or Typename
If TypeOf myRange.Cells(i, j).Value is String Then

' expects Then or GoTo
If TypeOf myRange.Cells(i, j).Value is GetType(String) Then

' expects Then or GoTo
Dim word As String
word = "Hello World"
If TypeOf myRange.Cells(i, j).Value is GetType(word) Then

我设法用

解决了问题
If Application.WorksheetFunction.IsText(myRange.Cells(i, j).Value) Then

但如果有更好的选择请告诉我

TypeOf 仅用于对象。

要检查 String 变量,请使用 TypeNameVarType:

Debug.Print TypeName(myRange.Cells(i, j).Value)
Debug.Print VarType(myRange.Cells(i, j).Value) 'VarType returns 8 for a string type

请参阅 here 所有变量类型在使用 VarType 的情况下返回的值...