Solved

Setting variables as integers or strings?

Posted on 2007-04-05
7
202 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
  • 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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
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…
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

707 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now