Check if a string variable has an integer value

103,417

Solution 1

A very simple trick is to try parse the string as an Integer. If it succeeds, it is an integer (surprise surprise).

Dim childAgeAsInt As Integer
If Integer.TryParse(childAge, childAgeAsInt) Then
    ' childAge successfully parsed as Integer
Else
    ' childAge is not an Integer
End If

Solution 2

Complementing Styxxy's response, if you dont need a result just replace it by vbNull:

If Integer.TryParse(childAge, vbNull) Then

Solution 3

You could perform the following two tests to be reasonably certain that the input you're getting is an integer:

If IsNumeric(childAge) AndAlso (InStr(1, childAge, ".") <> 0) Then
    fmSecA2 = "Wow! You are already " & childAge & " years old? You're growing to be a big " & childGender & " now! "
    If childAge < 0 OrElse childAge > 150 Then
        fmSecA2 = "I don't believe it's possible to be" & childAge & " years old..."
    End If
Else
    fmSecA2 = "Erm, I couldn't really understand your age. Are you making this up? Ho ho ho!"

The InStr function returns zero if it doesn't find the string that is being looked for, and so when combining that test with IsNumeric, you also rule out the possibility that some floating point data type was entered.

Solution 4

IsNumeric is built into VB, and will return a true/false

If IsNumeric(childAge) AndAlso (childAge > 0 And childAge < 150) Then
    fmSecA2 = "Wow! You are already " & childAge & " years old? You're growing to be a big " & childGender & " now! "
Else
    fmSecA2 = "Erm, I couldn't really understand your age. Are you making this up? Ho ho ho!"
End If

Solution 5

You can use this.

Sub checkInt() 
    If IsNumeric(Range("A1")) And Not IsEmpty(Range("A1")) Then 

        If Round(Range("A1"), 0) / 1 = Range("A1") Then 
            MsgBox "Integer: " & Range("A1") 
        Else 
            MsgBox "Not Integer: " & Range("A1") 
        End If 
    Else 
        MsgBox "Not numeric or empty" 
    End If 
End Sub 
Share:
103,417

Related videos on Youtube

Kai
Author by

Kai

Updated on July 09, 2020

Comments

  • Kai
    Kai almost 4 years

    I am working on a project which allows kids to send a message to Santa. Unfortunately, if they enter a string instead of an integer in the AGE field, the program crashes and returns Conversion from string "[exampleString]" to type 'Double' is not valid. Is there any way to check if they have entered an integer or not? This is the code.

    If childAge > 0 And childAge < 150 Then
        fmSecA2 = "Wow! You are already " & childAge & " years old? You're growing to be a big " & childGender & " now! "
    Else
        fmSecA2 = "Erm, I couldn't really understand your age. Are you making this up? Ho ho ho!"
    End If
    

    Thanks, Kai :)

  • tsuo euoy
    tsuo euoy over 11 years
    How does that answer the question?
  • bonCodigo
    bonCodigo over 11 years
    @Meta-Knight I am merely showing a sample code with GetType() to find out the variable Type. OP may use it to define whether it's an integer, long, boolean etc type. Sigh you downvote me :(
  • Jason Tyler
    Jason Tyler over 11 years
    This question was in regards to determining if a String can successfully parse to an Integer. Using GetType wouldn't help here. It would be of type String.
  • Kai
    Kai over 11 years
    Thanks! That really helped! :)
  • bonCodigo
    bonCodigo over 11 years
    @JasonTyler the title says "VB - Check if a variable is an integer"
  • Kai
    Kai over 11 years
    Thanks, that's quite helpful!
  • Kai
    Kai over 11 years
    A bit confusing, but ok. Thanks for helping! :)
  • bonCodigo
    bonCodigo over 11 years
    @Meta-Knight there are two other answers checking on integer here! So it's not very fair to downvote my answer when I am providing a proper way of checking the Type. Passing the String as an Integer is primary, however checking the Type is also within the question.
  • Dman
    Dman over 8 years
    If the child puts in a number and letters this will still bug out
  • secelite
    secelite over 7 years
    While this code snippet may solve the question, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.
  • Poat
    Poat over 6 years
    this assumes '.' as a decimal separator - which may be ok in most cases. but may be better to use Globalization.CultureInfo.CurrentCulture.NumberFormat.Number‌​DecimalSeparato instead