Convert to numeric representation

Viewed 42

The question is how to make sure that if the entered value is empty (""), then exit the procedure, otherwise convert it to a numeric representation. How to do it?

Sub macrotest()
      Dim num As Integer
  Dim text As String
         num = InputBox("enter the number", "number input")
            Select Case num Mod 100
               Case 2, 4, 6, 8
               MsgBox "even number" & num & ""
               Case Else
               MsgBox "idk" & num & ""
               End Select
               
End Sub
2 Answers

InputBox feat. Mod

Option Explicit

Sub macrotest()
    Dim cValue As Variant
    Dim text As String
    cValue = InputBox("enter the number", "number input")
    If IsNumeric(cValue) Then
        If cValue = CLng(cValue) Then ' if is whole number
            If cValue Mod 2 = 0 Then
                MsgBox "Even number " & cValue
            Else
                MsgBox "Odd number " & cValue
            End If
'            Select Case cValue Mod 10
'            Case 0, 2, 4, 6, 8
'                MsgBox "Even number " & cValue
'            Case Else
'                MsgBox "Odd number " & cValue
'            End Select
        Else
            MsgBox "'" & cValue & "' is not a whole number.", _
                vbExclamation, "Decimal Number"
        End If
    End If
End Sub

I modified your code a little to show how to test and convert user input:

Sub macrotest()
   Dim num As Integer
   Dim text As String
         
   text = InputBox("enter the number", "number input")
   
   If Not IsNumeric(text) Then Exit Sub
      
   num = CInt(text)
   
   Select Case num Mod 100
      Case 2, 4, 6, 8
         MsgBox "even number" & num & ""
      Case Else
         MsgBox "idk" & num & ""
   End Select
End Sub
Related