VBA:输入框和取消按钮 [英] VBA: inputbox and cancel button

查看:609
本文介绍了VBA:输入框和取消按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

…,如果您输入0Falsch(在德语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")会自动转换为布尔值.但是只有 Cancel 按钮会返回一个真正的布尔值.

This hapens because a string of 0 or "Falsch" (or "False") automatically casts into a boolean if compared with False. But only the Cancel button returns a true boolean.

这篇关于VBA:输入框和取消按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆