当前位置:网站首页>VBA: Inputbox Function and Inputbox Method
VBA: Inputbox Function and Inputbox Method
2022-08-10 09:40:00 【用户9949496】
文章背景:在编写VBA代码时,有时需要一个用户输入的对话框,以实现跟用户的交互。Inputbox函数是VBA自带的,功能相对简单;使用Application对象的inputbox方法,也可显示一个接收用户输入的对话框。接下来分别对两者进行介绍。
Inputbox Function
1.1 功能
Displays a prompt in a dialog box, waits for the user to input text or click a button, and returns a String containing the contents of the text box.
1.2 语法
InputBox(prompt, [ title ], [ default ], [ xpos ], [ ypos ], [ helpfile, context ])
- prompt : Required. String expression displayed as the message in the dialog box.
- title : Optional. String expression displayed in the title bar of the dialog box.
- default : Optional. 默认值。
- xpos, ypos : Optional. 对话框出现的位置。
- helpfile, context :Optional. 帮助文件。 注意:If the user chooses OK or presses ENTER, the InputBox function returns whatever is in the text box. If the user chooses Cancel, the function returns a zero-length string ("").
1.3 代码举例
Sub test()
Dim result As Variant
result = InputBox("Enter a number!")
MsgBox result
End Sub
如果选择对话框中的“取消”键,result返回的是空字符串。
Sub test()
Dim result As Integer
result = InputBox("Enter a number!")
MsgBox result
End Sub
如果选择对话框中的“取消”键,程序报错。原因为:在此处,result的数据类型为“Integer”,而InputBox返回的是空字符串,两者数据类型不符。
视频演示:http://mpvideo.qpic.cn/0bf2ciab2aaa24afdbtlknpvaewddujaahia.f10002.mp4?
Application.InputBox method
2.1 功能
Displays a dialog box for user input. Returns the information entered in the dialog box.
2.2 语法
expression.InputBox (Prompt, Title, Default, Left, Top, HelpFile, HelpContextID, Type)
- expression : A variable that represents an Application object.
- Type : Optional. Variant. Specifies the return data type. If this argument is omitted, the dialog box returns text. The following table lists the values that can be passed in the Type argument. Can be one or a sum of the values. For example, for an input box that can accept both text and numbers, set Type to 1 + 2.
注意:The dialog box has an OK button and a Cancel button. If you select the OK button, InputBox returns the value entered in the dialog box. If you select the Cancel button, InputBox returns False.
If Type is 8, InputBox returns a Range object. You must use the Set statement to assign the result to a Range object, as shown in the following example.
Set myRange = Application.InputBox(prompt := "Sample", type := 8)
If you do not use the Set statement, the variable is set to the value in the range, rather than the Range object itself.
两者的比较
The InputBox method differs from the InputBox function in that it allows selective validation of the user's input, and it can be used with Excel objects, error values, and formulas. Notice that Application.InputBox
calls the InputBox method; InputBox
with no object qualifier calls the InputBox function.
视频演示:http://mpvideo.qpic.cn/0bf2daab4aaa7uafe6tlkbpvaggddymaahqa.f10002.mp4?
Application.InputBox
具有数据验证功能,如果数据类型设置为单元格,输入的是数字或字符串,则会出现提醒,让用户重新输入。
参考资料:
[1] Coursera课程(Excel/VBA for Creative Problem Solving, Part 2)
[2] InputBox function(https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/inputbox-function)
[3] Application.InputBox method (Excel)(https://docs.microsoft.com/en-us/office/vba/api/excel.application.inputbox)
边栏推荐
- FPGA时钟篇(一) 7系列的时钟结构
- 傅立叶级数与傅里叶变换
- 91.(cesium之家)cesium火箭发射模拟
- OLTP and OLAP database architecture 】 【 : actual comparison
- 线程池的基本概念、结构、类
- Basic concepts, structures, and classes of thread pools
- 支付 x 聚合 x 分账 - 回流平台“二清”风险规避之路
- 原型和原型链
- 「应用架构」TOGAF建模:应用程序迁移图
- Defending risks with technology and escorting cloud native | Tongchuang Yongyi X Boyun held a joint product launch conference
猜你喜欢
jq封装树形下拉选择框组件
「微服务架构」编曲与编舞——让系统协同工作的不同模式
Defending risks with technology and escorting cloud native | Tongchuang Yongyi X Boyun held a joint product launch conference
shell遍历文件夹并输出
1 活动时间与安排
DeepFake换脸诈骗怎么破?让他侧个身
FPGA时钟篇(一) 7系列的时钟结构
Basic concepts, structures, and classes of thread pools
原型和原型链
腾讯发布四足机器人 Max 二代版本,梅花桩上完成跳跃、空翻
随机推荐
解决问题目录
2022年固定资产管理系统的概况
LCD DRM component 框架分析
PostgreSQL 2022 发展现状:13 个非 psql 工具
多线程浅谈
【元宇宙欧米说】听兔迷兔如何从虚拟到现实创造潮玩新时代
UE4 Sequence添加基础动画效果 (04-在序列中使用粒子效果)
Basic concepts, structures, and classes of thread pools
JWT: To own me is to have power
对称加密与非对称加密的区别
Lasso回归(Stata)
【Prometheus】Node Exporter常用查询PromQL 语句大总结
Static关键字及应用,继承的概念
「应用架构」TOGAF建模:应用程序迁移图
在“企业通讯录”的盲区,融云的边界与分寸
关于判断单峰数组的几种方法
第三章 搜索与图论(三)
07 【动态组件 组件注册】
shell遍历文件夹并输出
【REST架构】OData、JsonAPI、GraphQL 有什么区别?