VBA验证文本中是否存在文本,然后检查日期是否正确 [英] VBA to verify if text exists in a textbox, then check if date is in the correct format

查看:315
本文介绍了VBA验证文本中是否存在文本,然后检查日期是否正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个与VBA结合创建的excel表单。在表单中,我有一个用户要键入日期的文本框。



我创建了这个VBA子句,以确保用户在支持格式 xx / xx / xxxx。

 如果不是IsDate(textboxDate1.Text)然后
调用MsgBox(请选择正确的日期格式,vbOKOnly)
退出子
结束如果

但是,使用此VBA代码,用户需要输入日期,无论用户是否需要。所以当我有另外4个文本框以我的形式输入日期,用户只需要输入1个日期,而不是5,我有问题,用户需要在其他四个文本框的日期为了提交表单



所以我的问题:
什么VBA代码可用于第一个确定文本是否存在在文本框中,然后第二个来确定日期是否是正确的格式。



我正在尝试类似的内容:

 如果textboxDate1.ListIndex = -1然后

但我无法让它使用IsDate子句。



非常感谢提前。

解决方案

  If(Len(Trim(textboxDate1.Text)))0)而不是IsDate(textboxDate1.Text )然后
调用MsgBox(请选择正确的日期格式,vbOKOnly)
退出子
结束如果


I have an excel form that I created in combination with VBA. In the form I have a textbox in which the user is to type in a date.

I have created this VBA clause to ensure the user types in a date that supports the format xx/xx/xxxx.

If Not IsDate(textboxDate1.Text) Then
  Call MsgBox("Please select a correct Date format", vbOKOnly)
  Exit Sub
End If

However, with this VBA code, the user is required to enter a date, whether the user needs to or not. So when I have a 4 other textboxes to input a date in my form, and the user only needs to enter in 1 date, and not 5, I have the problem where the user is required to put in a date for the other four textboxs in order submit the form.

So my question: What VBA code is available to first determine whether text exists in the textbox, and then second to determine whether the date is in the correct format or not.

I was trying something similar to this:

 If textboxDate1.ListIndex = -1 Then

but I couldn't get it to work with the IsDate clause.

Many thanks in advance.

解决方案

If (Len(Trim(textboxDate1.Text)) <> 0)  And Not IsDate(textboxDate1.Text) Then 
  Call MsgBox("Please select a correct Date format", vbOKOnly) 
  Exit Sub 
End If 

这篇关于VBA验证文本中是否存在文本,然后检查日期是否正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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