Sunday, June 19, 2016

EXCEL VBA: How to only accept number format in a VBA Userform Textbox

To accept only numbers in a VBA Userform Textbox for example, use the following code:



Private Sub TextBox1_Change()
    OnlyNumbers
End Sub

Private Sub OnlyNumbers()
    If TypeName(Me.ActiveControl) = "TextBox" Then
        With Me.ActiveControl
            If Not IsNumeric(.Value) And .Value <> vbNullString Then
                MsgBox "Sorry, only numbers allowed"
                .Value = vbNullString
            End If
        End With
    End If
End Sub

1 comment:

  1. I admit, I have not been on this web page in a long time... however it was another joy to see It is such an important topic and ignored by so many, even professionals. professionals. I thank you to help making people more aware of possible issues.
    excel vba courses

    ReplyDelete