I wrote the following code which creates a 3 by 3 table and then sorts the columns from smallest to biggest depending on the values in row 1 (second row). For some reason, with the values below I get to a point where the code does "If( 2>1) Then" but the code then says its FALSE and skips, I have no idea why. Can anyone see what the issue may be?
First it does 1>2 which returns false, then 1>0 which return true and swaps the columns over, then it does 2>1 but returns FALSE, I can not find out why.
Thanks in advance!
VBA Code:
Sub SortButton()
Dim MyArray As Variant
ReDim MyArray(0 To 2, 0 To 2) As Variant
Dim x As Integer, y As Integer, z As Integer
Dim str0 As String, str1 As String, str2 As String
MyArray(0, 0) = MC
MyArray(0, 1) = Branch1
MyArray(0, 2) = Branch2
MyArray(1, 0) = 1
MyArray(1, 1) = 2
MyArray(1, 2) = 0
For x = LBound(MyArray, 2) To UBound(MyArray, 2) - 1
Sheets("Sheet1").Range("A9") = x
For y = (x + 1) To UBound(MyArray, 2)
Sheets("Sheet1").Range("A9") = x
Sheets("Sheet1").Range("A10") = y
Sheets("Sheet1").Range("A11") = MyArray(1, x)
Sheets("Sheet1").Range("C11") = MyArray(1, y)
If MyArray(1, x) > MyArray(1, y) Then
str0 = MyArray(0, x)
str1 = MyArray(1, x)
MyArray(0, x) = MyArray(0, y)
MyArray(1, x) = MyArray(1, y)
MyArray(0, y) = str0
MyArray(1, y) = str1
str0 = ""
str1 = ""
End If
Next y
Next x
Sheets("Sheet1").Range("A1") = MyArray(0, 0)
Sheets("Sheet1").Range("B1") = MyArray(0, 1)
Sheets("Sheet1").Range("C1") = MyArray(0, 2)
Sheets("Sheet1").Range("A2") = MyArray(1, 0)
Sheets("Sheet1").Range("B2") = MyArray(1, 1)
Sheets("Sheet1").Range("C2") = MyArray(1, 2)
End Sub
First it does 1>2 which returns false, then 1>0 which return true and swaps the columns over, then it does 2>1 but returns FALSE, I can not find out why.
Thanks in advance!
Last edited by a moderator: