Formatting MM/DD/YYYY dates in textbox in VBA

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

    Add something to track the length and allow you to do "checks" on whether the user is adding or subtracting text. This is currently untested but something similar to this should work (especially if you have a userform).

    'add this to your userform or make it a static variable if it is not part of a userform
    private oldLength as integer
    
    Private Sub txtBoxBDayHim_Change()
        if ( oldlength > txboxbdayhim.textlength ) then
            oldlength =txtBoxBDayHim.textlength
            exit sub
        end if
    
        If txtBoxBDayHim.TextLength = 2 or txtBoxBDayHim.TextLength = 5 then
        txtBoxBDayHim.Text = txtBoxBDayHim.Text + "/"
        end if
        oldlength =txtBoxBDayHim.textlength
    End Sub
    

提交回复
热议问题