Validation message of text box entry on modeless form interrupts text selection

前端 未结 3 555
我寻月下人不归
我寻月下人不归 2021-01-03 08:44

Hi I try this code in my Userform to check if Data entered in textbox1 is a number and if is not show a msgbox to user and select text in tex

3条回答
  •  悲&欢浪女
    2021-01-03 09:20

    I vote for usmanhaq and CommonSense!

    just something to add: I've tried to implement similiar thing on one of my projects, I end up avoiding pop up another window. Just use a label to alert.

    And after i implement this i find this more userfriendly!

    Hope this helps!

    userform:

    Private Sub TextBox1_Change()
    If Not IsNumeric(TextBox1.Value) Then
        Label1.Caption = "NUMBER ONLY!"
        UserForm1.TextBox1.SetFocus
        UserForm1.TextBox1.SelStart = FirstNonDigit(TextBox1.Value) - 1
        UserForm1.TextBox1.SelLength = Len(TextBox1.Text)
    Else
         Label1.Caption = ""
    End If
    End Sub
    

    this function is funded online that would help highlight starting from the first non number

    Public Function FirstNonDigit(xStr As String) As Long
        Dim xChar As Integer
        Dim xPos As Integer
        Dim I As Integer
        Application.Volatile
        For I = 1 To Len(xStr)
            xChar = Asc(Mid(xStr, I, 1))
            If xChar <= 47 Or _
               xChar >= 58 Then
                xPos = I
                Exit For
            End If
        Next
        FirstNonDigit = xPos
    End Function
    

提交回复
热议问题