Giri
02-14-2011, 07:01 AM
Hi Everyone,
I've been trying to work out how to do permutations within VBA. I came across some code which achieved that. However, there are a few lines of code I don't understand (The entire code is at the bottom)
1)
' PERMUTE -- the important part!
For Index = 0 To UBound(myArray)
' pick a random partner
OtherIndex = Rnd() * UBound(myArray)
' swap it with the current entry
TempStr = myArray(OtherIndex)
myArray(OtherIndex) = myArray(Index)
myArray(Index) = TempStr
Next Index
Could someone please explain to me how these few lines of codes work? It doesn't really make sense to me... I feel like it is simply going around in circles...
Thanks for all your help!
Kind Regards,
Giri
Entire Code:
Option Explicit
Public Sub Comb()
Dim myArray(10)
Dim Index As Integer
Dim msg As String
Dim OtherIndex As Integer
Dim TempStr As String
myArray(1) = Range("I14").Value
myArray(2) = Range("I15").Value
myArray(3) = Range("I16").Value
myArray(4) = Range("I17").Value
myArray(5) = Range("I18").Value
myArray(6) = Range("J14").Value
myArray(7) = Range("J15").Value
myArray(8) = Range("J16").Value
myArray(9) = Range("J17").Value
myArray(10) = Range("J18").Value ' This range was just filled with the letters "A" to "J"
' show starting state
For Index = 0 To UBound(myArray)
msg = msg & myArray(Index)
Next Index
MsgBox "Starting array: " & msg
' PERMUTE -- the important part!
For Index = 0 To UBound(myArray)
' pick a random partner
OtherIndex = Rnd() * UBound(myArray)
' swap it with the current entry
TempStr = myArray(OtherIndex)
myArray(OtherIndex) = myArray(Index)
myArray(Index) = TempStr
Next Index
' show ending state
msg = ""
For Index = 0 To UBound(myArray)
msg = msg & myArray(Index)
Next Index
MsgBox "Ending array: " & msg
End Sub
I've been trying to work out how to do permutations within VBA. I came across some code which achieved that. However, there are a few lines of code I don't understand (The entire code is at the bottom)
1)
' PERMUTE -- the important part!
For Index = 0 To UBound(myArray)
' pick a random partner
OtherIndex = Rnd() * UBound(myArray)
' swap it with the current entry
TempStr = myArray(OtherIndex)
myArray(OtherIndex) = myArray(Index)
myArray(Index) = TempStr
Next Index
Could someone please explain to me how these few lines of codes work? It doesn't really make sense to me... I feel like it is simply going around in circles...
Thanks for all your help!
Kind Regards,
Giri
Entire Code:
Option Explicit
Public Sub Comb()
Dim myArray(10)
Dim Index As Integer
Dim msg As String
Dim OtherIndex As Integer
Dim TempStr As String
myArray(1) = Range("I14").Value
myArray(2) = Range("I15").Value
myArray(3) = Range("I16").Value
myArray(4) = Range("I17").Value
myArray(5) = Range("I18").Value
myArray(6) = Range("J14").Value
myArray(7) = Range("J15").Value
myArray(8) = Range("J16").Value
myArray(9) = Range("J17").Value
myArray(10) = Range("J18").Value ' This range was just filled with the letters "A" to "J"
' show starting state
For Index = 0 To UBound(myArray)
msg = msg & myArray(Index)
Next Index
MsgBox "Starting array: " & msg
' PERMUTE -- the important part!
For Index = 0 To UBound(myArray)
' pick a random partner
OtherIndex = Rnd() * UBound(myArray)
' swap it with the current entry
TempStr = myArray(OtherIndex)
myArray(OtherIndex) = myArray(Index)
myArray(Index) = TempStr
Next Index
' show ending state
msg = ""
For Index = 0 To UBound(myArray)
msg = msg & myArray(Index)
Next Index
MsgBox "Ending array: " & msg
End Sub