Check if a string variable has an integer value

2019-06-15 08:17发布

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 :)

8条回答
对你真心纯属浪费
2楼-- · 2019-06-15 08:32

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 
查看更多
乱世女痞
3楼-- · 2019-06-15 08:32
Dim Input 

 Input = TextBox1.Text
 If Input > 0 Then 
   ............................
   ............................
 Else 
   TextBox2.Text = "Please only enter positive integers"
 End If
查看更多
家丑人穷心不美
4楼-- · 2019-06-15 08:34

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

If Integer.TryParse(childAge, vbNull) Then
查看更多
我想做一个坏孩纸
5楼-- · 2019-06-15 08:40

In .Net you may use GetType() to determine the data type of a variable.

Dim n1 As Integer = 12
Dim n2 As Integer = 82
Dim n3 As Long = 12

Console.WriteLine("n1 and n2 are the same type: {0}",
                  Object.ReferenceEquals(n1.GetType(), n2.GetType()))
Console.WriteLine("n1 and n3 are the same type: {0}",
                  Object.ReferenceEquals(n1.GetType(), n3.GetType()))
' The example displays the following output: 
'       n1 and n2 are the same type: True 
'       n1 and n3 are the same type: False  

Based on the above sample you can write a code snippet:

If childAge.GetType() = "Integer" then '-- also use childAge.GetType().Name = "Int32"
  ' do something
End if
查看更多
欢心
6楼-- · 2019-06-15 08:44

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
查看更多
Juvenile、少年°
7楼-- · 2019-06-15 08:46

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.

查看更多
登录 后发表回答