Formatting MM/DD/YYYY dates in textbox in VBA

前端 未结 9 1770
故里飘歌
故里飘歌 2020-11-22 10:47

I\'m looking for a way to automatically format the date in a VBA text box to a MM/DD/YYYY format, and I want it to format as the user is typing it in. For instance, once the

9条回答
  •  眼角桃花
    2020-11-22 11:12

    You could use an input mask on the text box, too. If you set the mask to ##/##/#### it will always be formatted as you type and you don't need to do any coding other than checking to see if what was entered was a true date.

    Which just a few easy lines

    txtUserName.SetFocus
    If IsDate(txtUserName.text) Then
        Debug.Print Format(CDate(txtUserName.text), "MM/DD/YYYY")
    Else
        Debug.Print "Not a real date"
    End If
    

提交回复
热议问题