Formatting MM/DD/YYYY dates in textbox in VBA

前端 未结 9 1819
故里飘歌
故里飘歌 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:01

    Private Sub txtBoxBDayHim_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    If KeyAscii >= 48 And KeyAscii <= 57 Or KeyAscii = 8 Then 'only numbers and backspace
        If KeyAscii = 8 Then 'if backspace, ignores + "/"
        Else
            If txtBoxBDayHim.TextLength = 10 Then 'limit textbox to 10 characters
            KeyAscii = 0
            Else
                If txtBoxBDayHim.TextLength = 2 Or txtBoxBDayHim.TextLength = 5 Then 'adds / automatically
                txtBoxBDayHim.Text = txtBoxBDayHim.Text + "/"
                End If
            End If
        End If
    Else
    KeyAscii = 0
    End If
    End Sub
    

    This works for me. :)

    Your code helped me a lot. Thanks!

    I'm brazilian and my english is poor, sorry for any mistake.

提交回复
热议问题