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

Los*_*gle 1 string excel vba typeof

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

如果我运行一个包含

MsgBox (TypeName(myRange.Cells(i, j).Value))
Run Code Online (Sandbox Code Playgroud)

我收到一条消息,说 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
Run Code Online (Sandbox Code Playgroud)

Fan*_*uru 5

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
Run Code Online (Sandbox Code Playgroud)

在此处查看所有变量类型的返回值,以防使用VarType...