tiehaze
asked on
Setting variables as integers or strings?
I am trying to set up variables, and then run if statements using the variables. The problem that I am coming up with is VarC can be text or numbers and I am declaring them as strings. Therfore, the following is happening:
Assume VarA is 1 and VarC is 10
Assume r.Offset(0, VarA).Value = 5
Dim VarA(1 To 8) As String, VarC(1 To 8) As String
If r.Offset(0, VarA).Value < VarC Then ...
Because VarC is a string, it is not accepting 10 as an integer, but as text:
(If 5 < "10" then ...)
How do I get around this...?
Assume VarA is 1 and VarC is 10
Assume r.Offset(0, VarA).Value = 5
Dim VarA(1 To 8) As String, VarC(1 To 8) As String
If r.Offset(0, VarA).Value < VarC Then ...
Because VarC is a string, it is not accepting 10 as an integer, but as text:
(If 5 < "10" then ...)
How do I get around this...?
ASKER
Also, ignore the "(1 To 8)" for the variables... that is irrelevant in my question
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
How would you recomment doing it if VarC looked like the following
Ty
Jon
10
Math
4
How would I test VarC to determine if I need to convert it or not.
Ty
Jon
10
Math
4
How would I test VarC to determine if I need to convert it or not.
VBA does do a bit of internal converting when needed, but not always. For example, run this sub:
Sub tiehazeexample()
MsgBox "5" > 4
MsgBox "4" > 5
MsgBox 5 > "4"
MsgBox 4 > "5"
End Sub
and you'll see true,false,true,false as it should be.
Simply referring to the line:
If r.Offset(0, VarA).Value < VarC Then
How do you want to check if 5 > "Math" ? I may just be missing the point of what you're trying to do.
However, to answer your question directly, you could use the IsNumeric function to see if the variable contains a number or not:
MsgBox IsNumeric("A")
MsgBox IsNumeric("5")
Matt
Sub tiehazeexample()
MsgBox "5" > 4
MsgBox "4" > 5
MsgBox 5 > "4"
MsgBox 4 > "5"
End Sub
and you'll see true,false,true,false as it should be.
Simply referring to the line:
If r.Offset(0, VarA).Value < VarC Then
How do you want to check if 5 > "Math" ? I may just be missing the point of what you're trying to do.
However, to answer your question directly, you could use the IsNumeric function to see if the variable contains a number or not:
MsgBox IsNumeric("A")
MsgBox IsNumeric("5")
Matt
ASKER
Hard to explain, but the isnumeric will get the job done. Thanks!
I figured what you were really doing was not apparent in the question (work-related info or what not). Glad I could help though :)
ASKER