Idaho said:
I have a long list of items (about 500 items and growing) that
currently is sorted alphabetically. I want to change it so that the
order is random. (I don't want students to be able to guess the next
item.) Is there an easy way of doing this short of shuffling things
by hand?
I'm in Office 2003 and XP Professional.
Thanks.
Fred
You know there had to be a macro solution lurking somewhere. <g>
Sub RandomizeParas()
Dim oTbl As Table
Dim oCol As Column
Dim nRow As Long, randRow As Long
Dim maxRows As Long
Dim aNums() As Long
Dim tmp As Long
Dim srcDoc As Document
Dim destDoc As Document
Set srcDoc = ActiveDocument
Set destDoc = Documents.Add
' Copy the text into a new blanks document
' and turn it into a 1-column table.
With destDoc
.Range.InsertFile srcDoc.FullName
Set oTbl = .Range.ConvertToTable _
(Separator:=wdSeparateByParagraphs, _
NumColumns:=1, ApplyHeadingRows:=False)
End With
' no longer need the original to be open
srcDoc.Close SaveChanges:=wdDoNotSaveChanges
' Make an array with one unique number for each row
' of the table, and randomize the numbers' order
' by swapping each entry with some random entry.
Randomize ' different sequence of numbers each time
maxRows = oTbl.Rows.Count
ReDim aNums(maxRows - 1)
For nRow = 0 To maxRows - 1
aNums(nRow) = nRow
Next
For nRow = 0 To maxRows - 1
' pick a random entry...
randRow = CLng((maxRows - 1) * Rnd)
' this is the swap...
tmp = aNums(nRow)
aNums(nRow) = aNums(randRow)
aNums(randRow) = tmp
Next
' Add a new column to the right side of the table,
' fill it with the randomized numbers, sort the table
' on that column, then delete the new column and
' convert the rest back to (now randomized) text.
With oTbl
Set oCol = .Columns.Add
For nRow = 1 To .Rows.Count
oCol.Cells(nRow).Range.Text = CStr(aNums(nRow - 1))
Next
.Sort FieldNumber:=2, SortFieldType:=wdSortFieldNumeric, _
ExcludeHeader:=False
.Columns(2).Delete
.ConvertToText
End With
End Sub
--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.