check if isnumeric data in two different cell in vba

mehidy1437

Active Member
Joined
Nov 15, 2019
Messages
348
Office Version
  1. 365
  2. 2016
  3. 2013
Platform
  1. Windows
  2. Mobile
  3. Web
Hi
How can I check, if cell a1 or cell b1 contain numerics data/value or not in vba?
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
If IsNumeric(Sheet1.Range("A1").Value) = True Then MsgBox "The value in A1 is numeric"
 
Upvote 0
Sorry my question was a bit confusing, i believe.

It should be, if cell a1 or cell e1 is numeric or not? I don't want check others cell in between a1 & e1.
Hope its clear now.
 
Upvote 0
If IsNumeric(Sheet1.Range("A1").Value) = True or IsNumeric(Sheet1.Range("E1").Value) = True Then MsgBox "A1 or E1 is numeric."
 
Upvote 0
Thanks for your help.
Is it necessary to mention the Sheet name for IsNumeric? Actually I'm trying to avoid sheet name. If possible.
 
Upvote 0
This is giving me an error 5: for IsNumeric.

VBA Code:
Option Explicit

Sub CalculationOnSelctionRng()
  
   'Set up the variables.
    Dim Rng As Range
    Dim selectioncell As String
    Dim firstcolcellad As String
    Dim lastcolrowad As String
  
    Dim firstcolcell As Double
    Dim lastcolrow As Double
    Dim mUlitipleValue As Double
    Dim sUbtractValue As Double
    Dim sUmValue As Double
    Dim dVidedValue As Double
  
   Set Rng = Selection
  
   selectioncell = Rng.Address(0, 0)
  
     firstcolcellad = ActiveCell.Address(0, 0)
        lastcolrowad = Rng(Rng.count).Address(0, 0)
 If IsNumeric(Rng(" & firstcolcellad & ").Value) = True Or IsNumeric(Rng(" & lastcolrowad & ").Value) = True Then
 
    MsgBox "The value in RANGE is numeric"
 
Else

     firstcolcell = ActiveCell.Value
     lastcolrow = Rng(Rng.count).Value

        mUlitipleValue = firstcolcell * lastcolrow
        sUbtractValue = firstcolcell - lastcolrow
        sUmValue = firstcolcell + lastcolrow
        dVidedValue = firstcolcell / lastcolrow
    
    MsgBox "Cell =" & selectioncell & vbNewLine & _
    " " & vbNewLine & _
    "Subtract =" & sUbtractValue & vbNewLine & _
     "Multiple =" & mUlitipleValue & vbNewLine & _
     "Divide =" & dVidedValue & vbNewLine & _
     "Sum =" & sUmValue & vbNewLine
     End If
End Sub
 
Upvote 0
Got the solution as below.

VBA Code:
  Option Explicit

Sub CalculationOnSelctionRng()
  
   'Set up the variables.
    Dim Rng As Range
    Dim selectioncell As String
    Dim firstcolcellad As String
    Dim lastcolrowad As String
  
    Dim firstcolcell As Variant
    Dim lastcolrow As Variant

    Dim mUlitipleValue As Double
    Dim sUbtractValue As Double
    Dim sUmValue As Double
    Dim dVidedValue As Double
  
   Set Rng = Selection
  
   selectioncell = Rng.Address(0, 0)
  
     firstcolcellad = ActiveCell.Address(0, 0)
        lastcolrowad = Rng(Rng.count).Address(0, 0)
          firstcolcell = ActiveCell.Value
                lastcolrow = Rng(Rng.count).Value
 'If WorksheetFunction.IsNumber(firstcolcell)= True And WorksheetFunction.IsNumber(lastcolrow) = True Then
 If WorksheetFunction.IsNumber(firstcolcell) And WorksheetFunction.IsNumber(lastcolrow) = True Then
        
        mUlitipleValue = firstcolcell * lastcolrow
        sUbtractValue = firstcolcell - lastcolrow
        sUmValue = firstcolcell + lastcolrow
        dVidedValue = firstcolcell / lastcolrow
    
    MsgBox "Cell =" & selectioncell & vbNewLine & _
    " " & vbNewLine & _
    "Subtract =" & sUbtractValue & vbNewLine & _
     "Multiple =" & mUlitipleValue & vbNewLine & _
     "Divide =" & dVidedValue & vbNewLine & _
     "Sum =" & sUmValue & vbNewLine
 
 
Else
    MsgBox "The values in SELECTION is not number"

     End If
End Sub
 
Upvote 0
Sometimes it is not necessary to have many variables, you can directly perform the operations, for example:

VBA Code:
Sub CalculationOnSelctionRng_2()
  Dim Rng As Range, fCell As Variant, lCell As Variant
  Set Rng = Selection
  fCell = Rng.Cells(1).Value
  lCell = Rng(Rng.Count).Value
  If IsNumeric(fCell) And IsNumeric(lCell) = True Then
    MsgBox "Cell    " & vbTab & "= " & Rng.Address(0, 0) & vbCr & vbCr & _
           "Subtract" & vbTab & "= " & fCell - lCell & vbCr & _
           "Multiple" & vbTab & "= " & fCell * lCell & vbCr & _
           "Divide  " & vbTab & "= " & fCell / lCell & vbCr & _
           "Sum     " & vbTab & "= " & fCell + lCell & vbCr
  Else
    MsgBox "The values in SELECTION is not number"
  End If
End Sub
 
Upvote 0
Here is another way to write the macro...
VBA Code:
Sub CalculationOnSelctionRng_4()
  Dim X As Long, ArrSymbols As Variant, ArrWords As Variant
  ArrSymbols = Array(, , "-", "*", "/", "+")
  ArrWords = Array("Cell:" & vbTab & Selection.Address(0, 0), "", "Subtract:", "Multiply:", "Divide:", "Sum:")
  For X = LBound(ArrSymbols) + 2 To UBound(ArrSymbols)
    ArrWords(X) = ArrWords(X) & vbTab & Evaluate(Replace(Selection.Address(0, 0), ":", ArrSymbols(X)))
  Next
  MsgBox Join(ArrWords, vbLf)
End Sub
 
Upvote 0
Here is another way to write the macro...

Hi Rick, it is certainly another way to write the macro, but it does not solve the initial OP question "check if isnumeric data in two different cell in vba".
If the cells are not numeric, your macro sends an error: "Type Mismatch"
 
Upvote 0

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,631
Latest member
a_potato

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