LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7
Default Shuffle Array

Hello all,

I usually find the answers I'm looking for without having to post them. So
I'd like to thank you all for all the help you've given me, you have no idea
how helpful you've been.

Since I'm relatively new at this, I was wondering if there was a better way
to (pseudo)randomly shuffle an array than what I've come up with. The code
is posted below. If any of you have some advice, I'd love to hear it.
Thanks!

Sub BuildAlistArr()

Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False

Dim arrAList As Variant, arrRnd As Variant, arrBList As Variant
Dim i As Long, j As Long
Dim flag As Boolean
Dim x As Long, y As Long, z As Long

With Worksheets("Sheet1")
With .Range("AList")
ReDim arrAList(.Cells.Count - 1)
For i = LBound(arrAList) To UBound(arrAList)
arrAList(i) = .Cells(i + 1)
Next
End With

x = LBound(arrAList)
y = UBound(arrAList)
z = y - x

ReDim arrRnd(y)
ReDim arrBList(y)

Randomize
For i = x To y
Do
arrRnd(i) = Int((y - x + 1) * Rnd + x) 'Unique Random Number
For j = x To i
flag = False
If arrRnd(i) = arrRnd(j) And i < j Then
flag = True
Exit For
End If
Next
Loop Until Not flag
arrBList(i) = arrAList(arrRnd(i))
.Cells(i + 2, 3).Value = arrBList(i)
Next
End With
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
shuffle table of numbers WLRobi Excel Discussion (Misc queries) 1 April 17th 08 12:17 PM
how do you shuffle a range of numbers at random Ann Excel Worksheet Functions 1 August 30th 07 04:34 PM
How to Shuffle the data at ROW level. Anil Reddy Excel Discussion (Misc queries) 2 March 13th 07 12:58 PM
how do i shuffle the numbers in a table? inkap Excel Discussion (Misc queries) 2 October 3rd 05 02:44 AM
Shuffle the cells! M H Excel Programming 4 August 2nd 05 08:16 PM


All times are GMT +1. The time now is 08:34 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"