varInput = Application.InputBox("Text", "Title", Type:=2)
If varInput = "False" Then
Exit Sub
End If
如果取消按钮被按下,返回值是一个带有“False”的字符串。 但是在某些具有德语设置的计算机上我会得到“Falsch”!
应该如何处理?
最佳答案
您还必须始终测试变量的类型是否为 bool 值: VarType(varInput) = vbBoolean
当使用Application.InputBox
时.
Dim varInput As Variant
varInput = Application.InputBox("Text", "Title", Type:=2)
If VarType(varInput) = vbBoolean And varInput = False Then
Exit Sub
End If
如果您仅测试 False
...
If varInput = False Then
…那么如果你输入 0
也会退出 sub或Falsch
(德语 Excel)或 False
(英文Excel)。
发生这种情况是因为一串 0
或"Falsch"
(或 "False"
)如果与 False
比较,会自动转换为 bool 值。但只有 Cancel 按钮返回 true bool 值。
关于excel - VBA:输入框和取消按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55453256/