Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Setting variables as integers or strings?

Posted on 2007-04-05
7
Medium Priority
?
217 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
Industry Leaders: 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!

 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

618 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