Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Randomize the order of the contents of an array

I have a range of cells 5 * 5 (A1:E5) which have string contents in them at
present. I want to run a macro that randomly sorts the existing contents
into different cells - it has to be the order of the existing content that
is randomly re-ordered not the content itself. Think of it similar to a
bingo card with the contents being the text strings one, two,...., twenty
five - where I want to produce random outputs for printing. How would I do
this in VBA.

Cheers.


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10
Default Randomize the order of the contents of an array

On Sat, 12 Jul 2003 07:53:45 +0100, "Lee Wold"
wrote in microsoft.public.excel.programming:

I have a range of cells 5 * 5 (A1:E5) which have string contents in them at
present. I want to run a macro that randomly sorts the existing contents
into different cells - it has to be the order of the existing content that
is randomly re-ordered not the content itself. Think of it similar to a
bingo card with the contents being the text strings one, two,...., twenty
five - where I want to produce random outputs for printing. How would I do
this in VBA.

Cheers.


"Excel 2002 Power Programming with VBA", John Walkenbach, p.342:
"Randomizing a range"
"The RANGERANDOMIZE function ... accepts a range argument and returns
an array that consists of the input range - in random order."

--
Michael Bednarek, IT Manager, Tactical Global Management
Waterfront Pl, Brisbane 4000, Australia. "POST NO BILLS"
http://mcmbednarek.tripod.com/
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 493
Default Randomize the order of the contents of an array

one way:

Public Sub RandomizeRange()
Dim temp As Variant
Dim arr As Variant
Dim rng As Range
Dim i As Integer, i1 As Integer
Dim j As Integer, j1 As Integer

Set rng = Range("A1:E5")
arr = rng.Value
For i = UBound(arr, 1) To 1& Step -1&
For j = UBound(arr, 2) To 1& Step -1&
i1 = Int(Rnd() * i) + 1&
j1 = Int(Rnd() * j) + 1&
temp = arr(i, j)
arr(i, j) = arr(i1, j1)
arr(i1, j1) = temp
Next j
Next i
rng.Value = arr
End Sub



In article ,
"Lee Wold" wrote:

I have a range of cells 5 * 5 (A1:E5) which have string contents in them at
present. I want to run a macro that randomly sorts the existing contents
into different cells - it has to be the order of the existing content that
is randomly re-ordered not the content itself. Think of it similar to a
bingo card with the contents being the text strings one, two,...., twenty
five - where I want to produce random outputs for printing. How would I do
this in VBA.

Cheers.


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 493
Default Randomize the order of the contents of an array

While John's excellent routine will work very well, the OP would
have to have a copy of his book in order to use your reply. I
realize that everyone *should* have a copy, of course...



In article ,
Michael Bednarek wrote:

"Excel 2002 Power Programming with VBA", John Walkenbach, p.342:
"Randomizing a range"
"The RANGERANDOMIZE function ... accepts a range argument and returns
an array that consists of the input range - in random order."

Reply
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
does an array contain contents of cell cooper_yonk New Users to Excel 2 November 27th 10 01:29 PM
search an array in reverse (bottom to top) order Trainer_00 Excel Discussion (Misc queries) 5 December 20th 07 10:35 PM
arrange contents of a table in Alphabetical order in excelsheet janvi Excel Worksheet Functions 1 September 4th 06 10:05 AM
Transpose and order array numbers cradino Excel Worksheet Functions 2 October 1st 05 06:27 PM
How do I lookup a value in a array that is not in ascending order John Excel Worksheet Functions 6 June 20th 05 09:40 PM


All times are GMT +1. The time now is 06:38 AM.

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

About Us

"It's about Microsoft Excel"