?
Solved

Setting variables as integers or strings?

Posted on 2007-04-05
7
Medium Priority
?
215 Views
Last Modified: 2010-04-30
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...?


0
Comment
Question by:tiehaze
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 

Author Comment

by:tiehaze
ID: 18860140
Let me know if I can clear anything up... Thanks!
0
 

Author Comment

by:tiehaze
ID: 18860151
Also, ignore the "(1 To 8)" for the variables... that is irrelevant in my question
0
 
LVL 35

Accepted Solution

by:
mvidas earned 2000 total points
ID: 18860304
Hi tiehaze,

You can convert a string to a number using one of the convert functions
CLng(var)   <--converts to Long variable (no decimals)
CDbl(var)   <--converts to Double variable

The (1 to 8) however is not irrelevant, that makes them a fixed-size array but you're not referencing which index of the array you want to compare to, like VarA(2), etc.

Matt
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:tiehaze
ID: 18860314
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.
0
 
LVL 35

Expert Comment

by:mvidas
ID: 18860351
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
0
 

Author Comment

by:tiehaze
ID: 18860363
Hard to explain, but the isnumeric will get the job done. Thanks!
0
 
LVL 35

Expert Comment

by:mvidas
ID: 18860386
I figured what you were really doing was not apparent in the question (work-related info or what not). Glad I could help though :)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question