REAL-TIME

VBA Projects

Full Access with Source Code
  • Designed and Developed by PNRao

  • Full Access with VBA Source Code

  • Well Commented Codes Lines

  • Creative and Professional Design

Effortlessly
Manage Your Projects

120+ Project Management Templates

Seamlessly manage your projects with our powerful & multi-purpose templates for project management.

120+ PM Templates Includes:
  • 50+ Excel Templates

  • 50+ PowerPoint Templates

  • 25+ Word Templates

Share Post

The VBA IsNumeric function is used to determine if a given value is numeric or not. It returns a Boolean value (True or False) based on the evaluation of the given input. This function is particularly useful when working with user input, as it can help avoid errors from non-numerical data being processed.

VBA IsNumeric Function – Purpose, Syntax and Arguments

Syntax:

IsNumeric(expression)

Arguments:

  • expression: This is the value or expression that needs to be evaluated for its numeric status.

Example:

Dim input As String
input = InputBox("Enter a number:")
If IsNumeric(input) Then
  MsgBox "The input is a number."
Else
  MsgBox "The input is not a number."
End If

In this example, the user is prompted to enter a number. The IsNumeric function is used to check if the input is a number or not. If it is, a message box will display “The input is a number.” If it is not, the message box will display “The input is not a number.”

Remarks:

  • The IsNumeric function can evaluate both numerical and non-numerical expressions.
  • It returns a Boolean value (True or False) depending on the input.
  • If the expression contains commas, only the numbers before the first comma will be evaluated. For example, IsNumeric(“45,780”) will return True.
  • Empty strings (“”) and spaces (” “) will be evaluated as False.

Important Notes:

  • The IsNumeric function is not available in Excel by default. It is part of the VBA library and can be used in Excel by enabling the ‘Microsoft Visual Basic for Applications Extensibility’ reference.
  • When using IsNumeric to evaluate a string that contains a number with decimal places, it will be evaluated as True. For example, IsNumeric(“45.78”) will return True.
  • In some cases, the IsNumeric function may return unexpected results, such as True for a string that contains non-numerical characters. It is important to thoroughly test and validate your code when using this function.

In summary, the VBA IsNumeric function is a useful tool for determining if a given value or expression is numeric or not. It can be used to validate user input and avoid processing errors. Remember to always thoroughly test and validate your code when using this function to ensure expected results.

Understanding VBA IsNumeric Function with Examples

Example 1: Determine if a cell contains a numeric value

The IsNumeric function in VBA is used to check whether a given expression or value is numeric or not. This function returns a boolean value True if the expression is numeric and False if it is not. Let’s look at an example to better understand how this function works.

Sub checkNumeric()
Dim num As String, result As Boolean
num = Range("A1").Value 'assigning the value of A1 cell to the variable num
result = IsNumeric(num) 'checking if the value in num variable is numeric
MsgBox result 'displays the result in a message box
End Sub
  1. Sub checkNumeric() – This is the beginning of the VBA code and it identifies the start of a subroutine named checkNumeric.
  2. Dim num As String, result As Boolean – This declares two variables: num as a string and result as a boolean. We will use these variables to store the cell value and the result of IsNumeric function.
  3. num = Range(“A1”).Value – This line assigns the value of cell A1 to the variable num. You can change the cell reference according to your data.
  4. result = IsNumeric(num) – This line uses the IsNumeric function to check whether the value in the num variable is numeric or not. If it is numeric, the function will return True and if it is not, it will return False.
  5. MsgBox result – The MsgBox function displays the result in a message box. In this example, it will display either True or False depending on the value in cell A1.
  6. End Sub – This marks the end of the subroutine.

In this example, we are checking the value in a single cell. However, you can also pass a range of cells or a variable to the IsNumeric function to check if all the values in them are numeric or not.

Example 2: Check whether a user input is numeric or not

Another practical use of the IsNumeric function is to validate user input. In this example, we will prompt the user to enter a number and check whether the input is numeric or not using the IsNumeric function.

Sub checkUserInput()
Dim user_num As String, result As Boolean
user_num = InputBox("Enter a number") 'prompts the user to enter a number
result = IsNumeric(user_num) 'checks if the input is numeric
If result = True Then
MsgBox "The input is a numeric value"
Else
MsgBox "The input is not a numeric value"
End If
End Sub
  1. Sub checkUserInput() – This is the beginning of the VBA code and it identifies the start of a subroutine named checkUserInput.
  2. Dim user_num As String, result As Boolean – This declares two variables: user_num as a string to store the user input and result as a boolean to store the result of IsNumeric function.
  3. user_num = InputBox(“Enter a number”) – The InputBox function displays a prompt message for the user to enter a number and stores the input in the user_num variable.
  4. result = IsNumeric(num) – This line uses the IsNumeric function to check whether the user input is numeric or not and assigns the result to the result variable.
  5. If result = True Then – If the value in the result variable is True, then it means the input is numeric and the message “The input is a numeric value” will be displayed using the MsgBox function. Otherwise, the message “The input is not a numeric value” will be displayed.
  6. End Sub – This marks the end of the subroutine.

In this example, we are using an If…Then statement to handle the result of the IsNumeric function. You can also use a Select Case statement or other control structures to handle the result based on your specific requirements.

Example 3: Convert a string to a number using the IsNumeric function

Another useful application of the IsNumeric function is to convert a string to a number data type. In VBA, you cannot perform mathematical operations on a string data type, so you need to convert it to a number before doing any calculations. Let’s look at an example.

Sub stringToNumber()
Dim str_num As String, num As Integer
str_num = Range("A1").Value 'assigning the value of A1 cell to the variable str_num
If IsNumeric(str_num) = True Then 'checking if the value in str_num is numeric
num = CInt(str_num) 'converting the string to an integer using CInt function
MsgBox "The number is: " & num 'displays the converted number in a message box
Else
MsgBox "The value in cell A1 is not numeric"
End If
End Sub
  1. Sub stringToNumber() – This is the beginning of the VBA code and it identifies the start of a subroutine named stringToNumber.
  2. Dim str_num As String, num As Integer – This declares two variables: str_num as a string to store the cell value and num as an integer to store the converted value.
  3. str_num = Range(“A1”).Value – This line assigns the value of cell A1 to the str_num variable. You can change the cell reference according to your data.
  4. If IsNumeric(str_num) = True Then – The If…Then statement checks whether the value in the str_num variable is numeric or not using the IsNumeric function.
  5. num = CInt(str_num) – If the value in str_num is numeric, we use the CInt function to convert it to an integer and assign the result to the num variable.
  6. MsgBox “The number is: ” & num – The Num value is displayed in a message box along with the text “The number is: ” using the MsgBox function.
  7. Else – If the value in the str_num variable is not numeric, then the code jumps to the Else statement and displays the message “The value in cell A1 is not numeric” using the MsgBox function.
  8. End Sub -This marks the end of the subroutine.

In this example, we are using the IsNumeric function along with the If…Then statement to handle non-numeric values. You can also use other VBA functions like CLng or CDbl based on the specific data type you want to convert the string to.
The IsNumeric function is a versatile tool that is often used in VBA programming to validate data, convert data types, and perform various calculations based on the data type. Understanding how this function works and its different applications can help you write more efficient and error-free VBA code.

Effortlessly Manage Your Projects and Resources
120+ Professional Project Management Templates!

A Powerful & Multi-purpose Templates for project management. Now seamlessly manage your projects, tasks, meetings, presentations, teams, customers, stakeholders and time. This page describes all the amazing new features and options that come with our premium templates.

Save Up to 85% LIMITED TIME OFFER
Project Management Templates

All-in-One Pack
120+ Project Management Templates

Essential Pack
50+ PM Templates

Excel Pack
50+ Excel PM Templates

PowerPoint Pack
50+ Excel PM Templates

MS Word Pack
25+ Word PM Templates

Ultimate Project
Management Template
Ultimate Resource
Management Template
Project Portfolio
Management Templates
Categories: VBA FunctionsTags: , , , Last Updated: September 30, 2023

Leave A Comment