Solved

Setting variables as integers or strings?

Posted on 2007-04-05
7
205 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 Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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

Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

828 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