# VBA Test Variables for Text or Number



## outlawspeeder (Dec 19, 2022)

I am stuck


I have two Variables, varOld and varNew.  I need to do math or combine the text.

I am looking at:

If varOld <> "" and varNew <> "" then
varOut =  varold - varNew
cells(x,y)= varNew & "   " &  VarOut
end if

Is there a way to test for text or number and do an if,else...?


----------



## Z51 (Dec 19, 2022)

```
If IsNumeric(varOld) Then
```


```
If Not IsNumeric(varNew) Then
```


----------



## Joe4 (Dec 19, 2022)

Yep.  Take a look at the IsNumeric function.





						MS Excel: How to use the ISNUMERIC Function (VBA)
					

This Excel tutorial explains how to use the Excel ISNUMERIC function with syntax and examples. The Microsoft Excel ISNUMERIC function returns TRUE if the expression is a valid number. Otherwise, it returns FALSE.




					www.techonthenet.com


----------



## Macropod (Dec 19, 2022)

For example:


```
Dim varOld, varNew
varOld = InputBox("varOld")
varNew = InputBox("varNew")
If IsNumeric(varOld) = True And IsNumeric(varNew) = True Then
    MsgBox CSng(varOld) + CSng(varNew)
Else
    MsgBox varOld & " " & varNew
End If
```


----------



## Z51 (Dec 19, 2022)

@Macropod
I don't believe you need the "= True" part for either comparison.  IsNumeric returns a boolean True or False by itself.  If you want to use "If IsNumeric(var) = False" instead of "If Not IsNumeric(var)" that's fine.

It doesn't hurt to have it there, of course, you just get "If TRUE = TRUE And TRUE = TRUE" (or the other variations), rather than "If TRUE And TRUE" or "If TRUE And FALSE", etc.


----------



## Macropod (Dec 19, 2022)

Z51 said:


> @Macropod
> I don't believe you need the "= True" part for either comparison.  IsNumeric returns a boolean True or False by itself.  If you want to use "If IsNumeric(var) = False" instead of "If Not IsNumeric(var)" that's fine.
> 
> It doesn't hurt to have it there, of course, you just get "If TRUE = TRUE And TRUE = TRUE" (or the other variations), rather than "If TRUE And TRUE" or "If TRUE And FALSE", etc.


True - but hopefully it helps the OP to understand what is going on. Your own post didn't explain that IsNumeric returns a Boolean value. Plus I showed how to get the result the OP is after.


----------

