简体   繁体   English

VBA 文本框自动更正为日期格式

[英]VBA Textbox autocorrect to date format

I'm new to VBA.我是 VBA 的新手。 I have a Date textbox for a UserForm in excel.我在 excel 中有一个用户窗体的日期文本框。 I want to input, for example, 1/25, and when I press Enter, it will automatically change to 1/25/22.我想输入,比如1/25,按回车会自动变成1/25/22。 The year will automatically correct to the present year (2022 At the moment).年份将自动更正为当前年份(目前为 2022 年)。 It is similar to the date autocorrect when you put a date on an excel cell, but I want it to work on a Userform Textbox.当您将日期放在 excel 单元格上时,它类似于日期自动更正,但我希望它在用户窗体文本框上工作。

Lastly, when I put the wrong character, like a single number or letter, it will have a message box to alert me that I need to reinput最后,当我输入错误的字符时,例如单个数字或字母,它会出现一个消息框提醒我需要重新输入

Thank you very much!非常感谢!

You can set the ControlSource property to point to an EXCEL cell formatted as date.您可以将 ControlSource 属性设置为指向格式为日期的 EXCEL 单元格。 This cell can be in a hidden worksheet, and it can have a defined name.此单元格可以位于隐藏的工作表中,并且可以具有已定义的名称。 For example in my case I called cell A1 in Sheet1 as MyDateCell, and set the textbox's ControlSource to =MyDateCell using textbox->right-click-properties.例如,在我的例子中,我将 Sheet1 中的单元格 A1 称为 MyDateCell,并使用 textbox->right-click-properties 将文本框的 ControlSource 设置为 =MyDateCell。 Now the textbox behaves like its corresponding cell.现在文本框的行为与其对应的单元格一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM