IsNumeric and Variable

AlexaS

Board Regular
Joined
Oct 12, 2012
Messages
158
Hello,

What I'm trying to do is put in a bit of code to stop people who aren't paying attention from moving forward and really screwing things up (or starting a never-ending loop) in the workbook. I have a user-input variable (Entries) which is dimmed as a public variable with a data type of integer. My code is:

Code:
If IsNumeric(Entries) = 1 Then
    Delete
ElseIf IsNumeric(Entries) > 1 Then
    Do
    R = R + 1
        Delete
    Loop While Entries <> R
ElseIf Not IsNumeric(Entries) Then
    MsgBox "Your entry """ & Entries & """ is not numeric." & _
    Chr(13) & "Please try again."
    Exit Sub
End If

End Sub

Basically I want them to put in a numeric value only (1, 2, 3, 4... etc) and to return the message box if they enter anything with an alpha character (1a, 2M, 3F, 4d... etc), since it will loop infinitely if there is an alpha character in this part of the macro.

When it's dimmed as an integer it doesn't hit any part of the code and just skips straight to the End Sub. When it's dimmed as a string, it registers as Not IsNumeric instead of the IsNumeric.

Any help would be appreciated :)

Thanks!
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
How are you getting the value Entries? Is it off the spreadsheet or an inputbox or what?
 
Upvote 0
Can you post the rest of the code, including the input box part?
 
Upvote 0
Code:
Public Entries, R As Integer

Sub UserInputforDelete()

Sheets("Sheet1").Activate
Entries = InputBox("Please enter the number of lines you need to delete:")
If IsNumeric(Entries) = 1 Then
    Delete
ElseIf IsNumeric(Entries) > 1 Then
    Do
    R = R + 1
        Delete
    Loop While Entries <> R
ElseIf Not IsNumeric(Entries) Then
    MsgBox "Your entry """ & Entries & """ as the number of cost centers to enter is not numeric." & _
    Chr(13) & "Please try again."
    Exit Sub
End If

End Sub
 
Upvote 0
Code:
Public Entries, R As Integer

Sub UserInputforDelete()

Sheets("Sheet1").Activate
Entries = InputBox("Please enter the number of lines you need to delete:")
If IsNumeric(Entries) = 1 Then
    Delete
ElseIf IsNumeric(Entries) > 1 Then
    Do
    R = R + 1
        Delete
    Loop While Entries <> R
ElseIf Not IsNumeric(Entries) Then
    MsgBox "Your entry """ & Entries & """ as the number of cost centers to enter is not numeric." & _
    Chr(13) & "Please try again."
    Exit Sub
End If

End Sub
Try this code instead...

Rich (BB code):
Sub UserInputforDelete()
  Dim Entries As Variant, DoAgain As Boolean, R As Long
  
  Do
    DoAgain = False
    Entries = InputBox("Please enter the number of lines you need to delete:")
    If Entries = "" Then
      MsgBox "You chose not to enter an integer, so the program ends here!"
      Exit Sub
    End If
    If Entries = 0 Then
      MsgBox "Your entry must be a positive number larger than 0." & vbLf & vbLf & _
             "Please try again."
      DoAgain = True
    ElseIf Entries Like "*[!0-9]*" Then
      MsgBox "Your entry """ & Entries & """ as the number of cost centers" & _
             "to enter is not a positive integer." & vbLf & vbLf & "Please try again."
      DoAgain = True
    End If
  Loop While DoAgain
  
Do
    R = R + 1
    Delete
  Loop While Entries <> R

End Sub

Note: The code I highlighted in red guarantees the entry is whole number (integer) only... not even a floating point number nor a negative number will pass it (theoretically, the empty string "" will pass it, but that condition was handled earlier in the code); the code I highlighed in green is taken directly from your posted code and I have no idea if it will work or not, but all the code will successfully get you to that point of your program with a positive integer value in the Entries variable. I would suggest, though, that maybe you put in some code to check if the Entries value is too large for your actual situation (that is, for an extreme case, protect against someone entering 1234567890 as a value).
 
Last edited:
Upvote 0
Code:
ElseIf Entries > 1 And Not Entries Like [COLOR=#ff0000]"*[!0-9]*" [/COLOR]Then
    Do
    R = R + 1
        DeleteGetCCNumber
    Loop While Entries <> R
ElseIf Entries Like [COLOR=#ff0000]"*[!0-9]*" [/COLOR]Then

Modified a bit, thanks Rick!

Could you explain your bit in red though? I've never seen that before today (although I did see it earlier in my searches).
 
Upvote 0
Code:
ElseIf Entries > 1 And Not Entries Like [COLOR=#ff0000]"*[!0-9]*" [/COLOR]Then
    Do
    R = R + 1
        DeleteGetCCNumber
    Loop While Entries <> R
ElseIf Entries Like [COLOR=#ff0000]"*[!0-9]*" [/COLOR]Then

Modified a bit, thanks Rick!

Could you explain your bit in red though? I've never seen that before today (although I did see it earlier in my searches).
I have this "thing" against the IsNumeric function, so many years ago (back in my volunteering days for the compiled version of VB) I used to post this message to the then newsgroups that covered user questions before forums became popular, I think it kind of explains the "why" of it...

From a previous post of mine...

I usually try and steer people away from using IsNumeric to "proof" supposedly numeric text. Consider this (also see note below):

Rich (BB code):
ReturnValue = IsNumeric("($1,23,,3.4,,,5,,E67$)")

Most people would not expect THAT to return True. IsNumeric has some "flaws" in what it considers a proper number and what most programmers are looking for.

I had a short tip published by Pinnacle Publishing in their Visual Basic Developer magazine that covered some of these flaws. Originally, the tip was free to view but is now viewable only by subscribers.. Basically, it said that IsNumeric returned True for things like -- currency symbols being located in front or in back of the number as shown in my example (also applies to plus, minus and blanks too); numbers surrounded by parentheses as shown in my example (some people use these to mark negative numbers); numbers containing any number of commas before a decimal point as shown in my example; numbers in scientific notation (a number followed by an upper or lower case "D" or "E", followed by a number equal to or less than 307 -- the maximum power of 10 in VB); and Octal/Hexadecimal numbers (&H for Hexadecimal, &O or just & in front of the number for Octal).

NOTE:
======
In the above example and in the referenced tip, I refer to $ signs and commas and dots -- these were meant to refer to your currency, thousands separator and decimal point symbols as defined in your local settings -- substitute your local regional symbols for these if appropriate.

As for your question about checking numbers, here are two functions that I have posted in the past for similar questions..... one is for digits only and the other is for "regular" numbers (the code is simple enough that it can be pulled from the function "housing" and used directly inside your own code):

Rich (BB code):
Function IsDigitsOnly(Value As String) As Boolean
    IsDigitsOnly = Len(Value) > 0 And Not Value Like "*[!0-9]*"
End Function

Function IsNumber(ByVal Value As String) As Boolean
    '   Leave the next statement out if you don't
    '   want to provide for plus/minus signs
    If Value Like "[+-]*" Then Value = Mid$(Value, 2)
    IsNumber = Not Value Like "*[!0-9.]*" And Not Value Like "*.*.*" And Len(Value) > 0 And Value <> "."
End Function

Here are revisions to the above functions that deal with the local settings for decimal points (and thousand's separators) that are different than used in the US (this code works in the US too, of course).

Rich (BB code):
Function IsNumber(ByVal Value As String) As Boolean
  Dim DP As String
  '   Get local setting for decimal point
  DP = Format$(0, ".")
  '   Leave the next statement out if you don't
  '   want to provide for plus/minus signs
  If Value Like "[+-]*" Then Value = Mid$(Value, 2)
  IsNumber = Not Value Like "*[!0-9" & DP & "]*" And Not Value Like "*" & _
             DP & "*" & DP & "*" And Len(Value) > 0 And Value <> DP
End Function

I'm not as concerned by the rejection of entries that include one or more thousand's separators, but we can handle this if we don't insist on the thousand's separator being located in the correct positions (in other words, we'll allow the user to include them for their own purposes... we'll just tolerate their presence).

Rich (BB code):
Function IsNumber(ByVal Value As String) As Boolean
  Dim DP As String
  Dim TS As String
  '   Get local setting for decimal point
  DP = Format$(0, ".")
  '   Get local setting for thousand's separator
  '   and eliminate them. Remove the next two lines
  '   if you don't want your users being able to
  '   type in the thousands separator at all.
  TS = Mid$(Format$(1000, "#,###"), 2, 1)
  Value = Replace$(Value, TS, "")
  '   Leave the next statement out if you don't
  '   want to provide for plus/minus signs
  If Value Like "[+-]*" Then Value = Mid$(Value, 2)
  IsNumber = Not Value Like "*[!0-9" & DP & "]*" And Not Value Like "*" & _
             DP & "*" & DP & "*" And Len(Value) > 0 And Value <> DP
End Function
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,237
Messages
6,170,924
Members
452,366
Latest member
TePunaBloke

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top