简体   繁体   中英

VBA Textbox autocorrect to date format

I'm new to VBA. I have a Date textbox for a UserForm in excel. I want to input, for example, 1/25, and when I press Enter, it will automatically change to 1/25/22. The year will automatically correct to the present year (2022 At the moment). 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.

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. 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. Now the textbox behaves like its corresponding cell.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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