excel - 与字符串比较时如何使用 "TypeOf ___ is ___ Then "

标签 excel vba string typeof

我想检查用户输入。如果我在 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

最佳答案

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 的情况下,所有变量类型都返回值...

关于excel - 与字符串比较时如何使用 "TypeOf ___ is ___ Then ",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68437193/

相关文章:

php - 在 PHP 中将字符串 [基本上是逗号分隔的数字] 转换为整数的最佳方法

Python - 从文件中读取先前的 'list' 变量

excel - 有合并单元格时使用find方法,vba excel

arrays - 突出显示变体数组中的单元格

excel - 在 VBA 中重新计算单元格或范围的不同方法

json - 检索包含 AJAX 内容的网页

vba - 如何在口袋妖怪游戏中添加怪物?

java - 在java中写入excel

python - 开始使用 python 处理数据

c - 查找子字符串 s2 在 s1 中的位置的函数