IsError Question

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
4,616
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I am trying to determine if a user inputted value (txt_freq) is a number or not using this code:

Rich (BB code):
txt_freq = "dsadfdfs"
If IsError(txt_freq * 1) Then
   freqval = False
   MsgBox "Invalid frequency (Non numeric)"
    Exit Do
End If

The line is throwing a "Type mismatch" error, rightly so as a mathematical function is being applied to a alphabetical variable. I'm assuming this is not how to catch this error?
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
I would use Isnumeric

VBA Code:
txt_freq = "dsadfdfs"
If Isnumeric(txt_freq)=false Then
   freqval = False
   MsgBox "Invalid frequency (Non numeric)"
    Exit Do
End If

Although if you are using an InputBox, you can force a numeric entry.
 
Upvote 0
I am trying to determine if a user inputted value (txt_freq) is a number or not using this code:
I would say that it depends on just what sort of "number" you are expecting, how rigorous you want/need to be and how weird the values entered by the user might be.
Mentioning those things because IsNumeric has some funny characteristics. For example if any of these values are used as txt_freq in the above code then no message box will appear yet I expect they are not the sort of values you would want to be entered & accepted.
"3,5,6$"
"4e4"
"23,,,,,.3"

There are plenty more such values that can be found.
 
Upvote 0
Hmmm. Learn something new everyday.


The worksheet function seems to work as expected.

VBA Code:
txt_freq = "4e4"
If Application.WorksheetFunction.IsNumber(txt_freq + 1) Then
   freqval = False
   MsgBox "Invalid frequency (Non numeric)"
    Exit Do
End If
 
Upvote 0
Hi Scott and Peter, thank you for your contributions.
Scott's suggestion will work, thank you. However, Peter, you are correct ... although highly unlikely, if it can happen it will eventually, that someone enters such a value that the IsNumeric function won't catch. In my full code, I have a half dozen other error traps that ensure that a properly formatted number is entered. My full code as I've been playing with can be found here if curious.

If indeed the Isnumeric fuction identifies the entry as infact being a number, is it a true number? Can I use it as is incalculations? Or would I need to convert the value to a number first? (I'm of the understanding that entries into an inputbox are strings).
 
Upvote 0
The worksheet function seems to work as expected.

VBA Code:
txt_freq = "4e4"
If Application.WorksheetFunction.IsNumber(txt_freq + 1) Then
   freqval = False
   MsgBox "Invalid frequency (Non numeric)"
    Exit Do
End If

Surely that would suffer the same "Type mismatch" error as reported in the OP if that same txt_freq input was used?
 
Upvote 0

Forum statistics

Threads
1,224,813
Messages
6,181,112
Members
453,021
Latest member
Justyna P

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