Showing posts with label authorized. Show all posts
Showing posts with label authorized. Show all posts

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