【VBA研究】如何检查文本框中输入的日期
来源:互联网 发布:星际淘宝网txt下载八零 编辑:程序博客网 时间:2024/05/22 10:29
iamlaosong文
应用中经常会对文本框的输入内容进行检查,如果不符合要求,给出提示,保留在文本框中重新输入,如何编程?
如果输入的是日期,如何检查?下面是一个输入日期的窗体,输入起止日期并进行检查,输入用8位数字,这样输入比较快,输入后用DateSerial转换成日期格式(DateSerial是个很有意思的函数,其功能是将参数年月日转换成一个有效的日期。虽然要求输入规范的年月日,不过,输入不规范也没关系,它可以将输入不规范的日期,转换成规范的日期,DateSerial(2017,04,31)转换成2017-5-1),界面如下图:
用文本框退出事件对日期进行检查,当日期有误需要留在文本框中时,令参数cancel=True即可,代码如下:
'输入日期,确定按钮Private Sub CommandButton1_Click() StartDate = TextBox1.Value EndDate = TextBox2.Value TextBox1.Value = "" TextBox2.Value = "" Input_Date.HideEnd Sub'输入日期,取消按钮Private Sub CommandButton2_Click() TextBox1.Value = "" TextBox2.Value = "" StartDate = "" EndDate = "" Input_Date.HideEnd Sub'输入日期,初始化文本框Private Sub TextBox1_Enter() If TextBox1.Value = "" Then TextBox1.Value = Format(Date, "yyyymmdd")End Sub'输入日期,离开起始日期Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean) If TextBox1.Value Like "########" Then TextBox2.Value = TextBox1.Value TextBox1.Value = DateSerial(Left(TextBox1.Value, 4), Mid(TextBox1.Value, 5, 2), Right(TextBox1.Value, 2)) Else '日期有误,留在输入框 MsgBox "日期有误,请重新输入!", vbOKOnly, "iamlaosong" TextBox1.Value = Format(Date, "yyyymmdd") Cancel = True End IfEnd Sub'输入日期,离开截止日期Private Sub TextBox2_Exit(ByVal Cancel As MSForms.ReturnBoolean) If TextBox2.Value Like "########" Then TextBox2.Value = DateSerial(Left(TextBox2.Value, 4), Mid(TextBox2.Value, 5, 2), Right(TextBox2.Value, 2)) If TextBox2.Value < TextBox1.Value Then MsgBox "截止日期不能小于起始日期!", vbOKOnly, "iamlaosong" Cancel = True End If Else MsgBox "日期有误,请重新输入!", vbOKOnly, "iamlaosong" TextBox2.Value = Format(TextBox1.Value, "yyyymmdd") Cancel = True End IfEnd Sub
0 0
- 【VBA研究】如何检查文本框中输入的日期
- vba 验证文本框中只输入数字
- 【VBA研究】循环中用VBA在单元格中输入公式
- 在两个文本框中输入日期数据后,第三个文本框能够自动计算出两个日期之间的天数
- 如何判断文本框中输入的电话号码的有效性
- 如何使文本框中输入的必须是数字?
- 如何判断文本框中输入的是空格
- Android中对文本框里的输入值进行检查过滤
- 检查用户在文本框中是否是连续输入的状态
- 【VBA研究】如何使用VBA项目的数字证书
- 动态检查用户在文本框中是否输入了文字
- javascript日期输入文本框
- 在JAVA中如何检查字符串日期的合法性
- 在JAVA中如何检查字符串日期的合法性
- 如何禁止在文本框中输入中文
- 如何使文本框中只能输入数字
- 如何限定文本框中只能输入汉字
- 如何让文本框中只能输入手机号码
- leetcode 151. Reverse Words in a String
- RocketMQ消息重复消费的问题
- 数据库优化方案
- 袁芳的学习笔记(6)基于Predix的在线分析服务
- 4 安装gdcm(用于读取压缩的dcm数据)
- 【VBA研究】如何检查文本框中输入的日期
- 一次性定时器
- TCP/IP
- Scapy的简单使用
- org.json.JSONException: End of input at character 0 of 异常
- hrbust 1133 MST (最小生成树)
- IDEA
- react native自动轮播图
- centos安装 / Installation of CentOS 7.3 Guide