gpt4 book ai didi

Excel VBA,如何仅制作文本框格式日期

转载 作者:行者123 更新时间:2023-12-04 21:56:37 25 4
gpt4 key购买 nike

我想制作一个文本框,但该文本框只能按日期格式输入,不能输入其他任何内容。

最佳答案

据我所知,您不能(轻松)强制 TextBox 中的文本始终为有效日期,但您可以检查用户尝试离开 TextBox 时输入的内容:

Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If Not IsDate(TextBox1.Text) Then
MsgBox "Date required"
Cancel = True
End If
'Display value in another textbox for testing purposes
TextBox2.Text = Format(CDate(TextBox1.Text), "dd/mm/yyyy")
End Sub

使用 IsDate将允许 任意 要输入的系统识别日期,因此您应该使用 CDate(TextBox1.Text)访问输入的日期。不要依赖文本本身采用特定格式,因为输入的文本可能是,例如:
  • "08/03/2017"
  • "8 March 2017"
  • "8 Mar"
  • "8/3/17"
  • 关于Excel VBA,如何仅制作文本框格式日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43290152/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com