巴打,想請教你少少問題,而家玩緊 nested loop task,但有度唔明就係 “Temp = myArray(j)” (task answer) 果度,有時啲task係會”Temp=myArray(i)” , 我試過兩個都會出同樣result, 出到ABCDE, 咁其實係咪兩個都得?
Sub vba_sort_array()
Dim myArray() As Variant
ReDim myArray(5)
Dim i As Integer
Dim j As Integer
Dim Temp As String
myArray(1) = "E"
myArray(2) = "D"
myArray(3) = "C"
myArray(4) = "B"
myArray(5) = "A"
'sorting array from A to Z
For i = LBound(myArray) To UBound(myArray)
For j = i + 1 To UBound(myArray)
If UCase(myArray(i)) > UCase(myArray(j)) Then
Temp = myArray(j)
myArray(j) = myArray(i)
myArray(i) = Temp
End If
' For j = i + 1 To UBound(myArray)
' If UCase(myArray(i)) > UCase(myArray(j)) Then
' Temp = myArray(i)
' myArray(i) = myArray(j)
' myArray(j) = Temp
' End If
'
Next j
Next i
Debug.Print myArray(1)
Debug.Print myArray(2)
Debug.Print myArray(3)
Debug.Print myArray(4)
Debug.Print myArray(5)
End Sub