0

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?

4
  • The problem could be that 150 and 3 are being treated as strings. Therefor 3 > 1. So just cast them to Integer before comparing. Commented Apr 18, 2018 at 8:46
  • @PaulKaram : excellent. Commented Apr 18, 2018 at 8:46
  • @PaulKaram Thanks for the answer. For everybody who doesnt know that: Val() function is for change a string Value to Integer Val(StringArray(1,5)) Commented Apr 18, 2018 at 8:50
  • @PatrickSchöpfer stackoverflow.com/help/someone-answers Commented Apr 18, 2018 at 8:52

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks. Val() does the same as i know
@PatrickSchöpfer Yeah it does the same and more. I included three links of other ways as well.
Convert.toInt32 and the "many other ways" link are non-VBA related. This is .Net
@Vityata Good point. I usually don't follow VBA so thing question must have been tagged with VB.NET before being edited. Anyways, I fixed that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.