Hi, Peter,
Your macro will fill the table with random numbers from 1 to 25, but
some of them may be duplicates while other numbers in the range may
not occur in the table. Instead, I got the impression that the request
was to put in each number from 1 to 25, but in random order. Here's a
macro to do that.
Sub RandomTable()
Dim RandArray(1 To 25) As Integer
Dim Idx As Integer, Idx2 As Integer
Dim Temp As Integer
Dim oTbl As Table
' check for existence of table
' with minimum size 5x5
If ActiveDocument.Tables.Count = 0 Then
' no table, so make one
Set oTbl = ActiveDocument.Tables.Add( _
Range:=Selection.Range, _
numrows:=5, numcolumns:=5)
Else
Set oTbl = ActiveDocument.Tables(1)
If (oTbl.Rows.Count < 5) Or _
(oTbl.Columns.Count < 5) Then
' too small -- complain and quit
MsgBox "Expected table to be at least 5x5."
Exit Sub
End If
End If
' start with an in-order list of numbers
For Idx = 1 To 25
RandArray(Idx) = Idx
Next Idx
' initialize the rnd generator
Randomize
' swap each entry with a
' randomly chosen other entry
For Idx = 1 To 25
Idx2 = Int(25 * Rnd + 1)
Temp = RandArray(Idx)
RandArray(Idx) = RandArray(Idx2)
RandArray(Idx2) = Temp
Next Idx
' put the entries into the table cells
' left to right, then down
For Idx = 1 To 5
For Idx2 = 1 To 5
oTbl.Cell(Row:=Idx, Column:=Idx2).Range.Text = _
RandArray(5 * (Idx - 1) + Idx2)
Next Idx2
Next Idx
End Sub