问题描述
varInput = Application.InputBox("Text", "Title", Type:=2)
If varInput = "False" Then
Exit Sub
End If
如果按下取消按钮,则返回值为带有"False"的字符串.但是在某些使用德语设置的计算机上,我会得到"Falsch"!
If the cancel button is pressed, the return value is a string with "False".But on some computer with german setting I will get "Falsch" !
该如何处理?
推荐答案
使用Application.InputBox
时,还必须始终将变量的类型测试为布尔值:VarType(varInput) = vbBoolean
.
You must always also test the type of the variable to be boolean: VarType(varInput) = vbBoolean
when using the 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
或Falsch
(在德语Excel中)或False
(在英语Excel中),它也会退出sub.
… then it will also exit sub if you enter 0
or Falsch
(in german Excel) or False
(in English Excel).
之所以会发生这种情况,是因为与False
相比,字符串0
或"Falsch"
(或"False"
)会自动转换为布尔值.但是只有按钮会返回一个真正的布尔值.
This hapens because a string of 0
or "Falsch"
(or "False"
) automatically casts into a boolean if compared with False
. But only the button returns a true boolean.
这篇关于VBA:输入框和取消按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!