Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Excel VBA - What are the rules for calculating strings? 1

Status
Not open for further replies.

knifey

Technical User
Nov 14, 2006
180
GB
Hi,
I'm currently writing some code that subtracts the previous months activity YTD from the current months activity YTD to derive the current monthly activity.
Then, in my code, if the current months activity YTD figure is less than the previous months activity YTD figure (i.e. a negative number) it fills the row pink.
My problem is that there seems to be no logic to what is turning the row pink (i.e. the rows are turning pink in a random fashion).
My two variables iActThisMonth and iActLastMonth are defined as strings because I am using the returned "" value in later code.
But my question is, how does Excel VBA handle numerical calculations on numbers stored as text?
My thinking is that if my two variables iActThisMonth and iActLastMonth are defined as strings. Is my < operator still comparing numbers? Or is it comparing ASCI characters (or maybe somthing else).
Any advise or a nudge in the right direction would be much appreciated.
Thanks,
Roy
 
my two variables iActThisMonth and iActLastMonth are defined as strings"
[TT]
"10" < "9"
[/TT]
If you have STRINGs and are doing a compare, they are compared as STRINGS.

Skip,
[glasses]Don't let the Diatribe...
talk you to death![tongue]

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Use the Val function:
If Val(iActThisMonth) < Val(iActLastMonth) Then

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Thanks PHV,
The Val function is exactly what I need. And Skip, I've now read up on binary and text compare (you learn something new every day).
Cheers,
Knifey
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top