Solved

Setting variables as integers or strings?

Posted on 2007-04-05
7
208 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 500 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
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!

 

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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying 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 can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
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.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

729 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