Compare String Arrays VBA

I have a 2 dimensional string array. In the string array are some strings and some integer numbers. I want to compare two positions of the array together.

Example:

If StringArray(1, 5) > StringArray(1, 6) Then
    MsgBox "Is first number is bigger"   
End if

My Debugger says:

StringArray(1,5) = 150
StringArray(1,6) = 3 
StringArray(1, 5) > StringArray(1, 6) = False

Does someone have an idea, why the Result is false? StringArray(1, 5) is definitely bigger. What could be the problem? Does it have something to do with the array type and how can i change that without creating a new integer array just for 2 Values?

Upvotes: 0

Views: 160

Answers (1)

Paul Karam
Paul Karam

Reputation: 4225

As you stated, your array type is String.
When you're comparing StringArray(1,5) to StringArray(1,6), you are actually comparing strings.

In your case, you are comparing 150 to 3 as strings. Hence the result is false, because "3" is > "1" and not vice verca.

In order to have it compare them as integers, you need to cast them to integers before. Like this:

If Val(StringArray(1, 5)) > Val(StringArray(1, 6))

There's other ways such as: CInt.

Upvotes: 2

Related Questions