Limit Value Of Userform Textbox To Positive Whole Numbers With VBA

Only Accept Positive Whole Number Values in Userform Textbox VBA

What This VBA Code Does

I just got done updating a VBA userform requiring a textbox that would only accept inputs of whole numbers (non-decimal) that were also positive in nature from the user. Here is what I came up with for my VBA code to handle this situation.

Only Allow Positive Whole Numbers

Private Sub TextBox1_Change()
'PURPOSE: Limit TextBox inputs to Postive Whole Numbers
'SOURCE: www.TheSpreadsheetGuru.com/the-code-vault

If Not IsNumeric(TextBox1.Text) And TextBox1.Text <> "" Then
  If Len(TextBox1.Text) > 1 Then
    'Remove Added Non-Numerical Character from Number
      TextBox1.Text = Abs(Round(Left(TextBox1.Text, Len(TextBox1.Text) - 1), 0))
  Else
    'Delete Single Non-Numerical Character
      TextBox1.Text = ""
  End If
ElseIf TextBox1.Text <> "" Then
  'Ensure Positive and No Decimals
    TextBox1.Text = Abs(Round(TextBox1.Text, 0))
End If

End Sub

Only Allow Positive Whole Numbers Greater Than 0

I later found out that I needed to go one step further and exclude zero from the list of acceptable inputs as well. Below is the following modification to prevent the value 0 from showing up in the userform textbox.

Private Sub TextBox1_Change()
'PURPOSE: Limit TextBox inputs to Postive Whole Numbers (excluding 0)
'SOURCE: www.TheSpreadsheetGuru.com/the-code-vault

If Not IsNumeric(TextBox1.Text) And TextBox1.Text <> "" Then
  If Len(TextBox1.Text) > 1 Then
    'Remove Added Non-Numerical Character from Number
      TextBox1.Text = Abs(Round(Left(TextBox1.Text, Len(TextBox1.Text) - 1), 0))
  Else
    'Delete Single Non-Numerical Character
      TextBox1.Text = ""
  End If
ElseIf TextBox1.Text <> "" Then
  If TextBox1.Text = 0 Then
    'Ensure Not Zero
      TextBox1.Text = ""
  Else
    'Ensure Positive and No Decimals
      TextBox1.Text = Abs(Round(TextBox1.Text, 0))
  End If
End If

End Sub

How Do I Modify This To Fit My Specific Needs?

Chances are this post did not give you the exact answer you were looking for. We all have different situations and it's impossible to account for every particular need one might have. That's why I want to share with you: My Guide to Getting the Solution to your Problems FAST! In this article, I explain the best strategies I have come up with over the years to getting quick answers to complex problems in Excel, PowerPoint, VBA, you name it

I highly recommend that you check this guide out before asking me or anyone else in the comments section to solve your specific problem. I can guarantee 9 times out of 10, one of my strategies will get you the answer(s) you are needing faster than it will take me to get back to you with a possible solution. I try my best to help everyone out, but sometimes I don't have time to fit everyone's questions in (there never seem to be quite enough hours in the day!).

I wish you the best of luck and I hope this tutorial gets you heading in the right direction!

~ Chris