I've been trying to shuffle an 11-integer array and paste the shuffled array into excel. I've found some code that almost does what I want, but instead of returning the shuffled entries of the array it shows the shuffled row numbers (Col A) and the random numbers used for sorting (Col B).
I'm new to VBA and can't figure out to return the entry of the array that corresponds to the shuffled row number in Col A, if that makes sense? I only want to see the shuffled entries and not the row numbers or random numbers. Hope that makes sense! I'm using:
Sub Shuffle()
Dim intNumbers(1 To 11) As Integer
'the list of numbers I want to shuffle
intNumbers(1) = 1
intNumbers(2) = 1
intNumbers(3) = 1
intNumbers(4) = 1
intNumbers(5) = 1
intNumbers(6) = 1
intNumbers(7) = 2
intNumbers(8) = 5
intNumbers(9) = 6
intNumbers(10) = 3
intNumbers(11) = 7
Dim rngNumbers As Range
Dim rngRandom As Range
Dim rngSort As Range
Dim rngTemp As Range
Set rngNumbers = ActiveSheet.Range("A1:A11")
Set rngRandom = ActiveSheet.Range("B1:B11")
Set rngSort = ActiveSheet.Range("A1:B11")
Randomize
' store number and random sequence
For Each rngTemp In rngRandom
rngTemp = Rnd()
rngTemp.Offset(0, -1) = rngTemp.Row
Next
rngSort.Sort key1:=rngSort.Columns(2)
For Each rngTemp In rngNumbers
intNumbers(rngTemp.Value) = rngTemp
Next
End Sub
I can see what this code is doing but can't figure out how to get it to do what I'd like. Still got a lot to learn!
Try this code. It will leave the original rows in column A, sorted random numbers A>Z in column B, and in column C: the index of your array, dependent on the row number.
Here's one way to make your code work:
Here are two approaches. The first is a somewhat naïve and not terribly efficient shuffle sub which I first used, oddly enough, when simulating the game of "Candyland". The sub takes a passed array and shuffles it by randomly swapping pairs of elements (for a default of 1000 times). The second sub illustrates some of the advantages of using variants to hold arrays in VBA and uses a standard trick which posts a 1-dimensional array of values into a column rage in 1 line of code. Every time you run it A1:A11 is given thos 11 elements in random order.
The second approach is more efficient and is given by a function rather than a sub. It shares the desirable feature of not needing to make any assumptions about the spreadsheet (e.g. columns B and C are available) and can also be thought of in terms of cards -- informally I think of it as the "52 pickup" shuffle ( https://en.wikipedia.org/wiki/52_Pickup ):