PDA

View Full Version : List Randomizer Macro for Word 2010



ARMaterials
04-19-2014, 02:21 PM
Way back with Word '97 one of my students wrote a convenient macro for randomizing lists of words. Briefly, the list was highlighted and the Macro button (or keystrokes) pushed - and the list was nicely randomized.

I do not have this Macro anymore and need a similar one for Office 2010 32 bit (MS Word only).

Note: I am well aware of online randomizers as well as using Excel to randomize a list. However, I wish to eliminate time/energy wasted steps and randomize word lists within Word 2010 itself.

Any suggestions?

macropod
04-19-2014, 09:01 PM
@ All: Cross-posted at: http://www.msofficeforums.com/word-vba/20651-list-randomizer-macro-word-2010-a.html#post62455
For cross-posting etiquette, please read: http://www.excelguru.ca/content.php?184

SamT
04-28-2014, 12:54 PM
ARMaterials (http://www.vbaexpress.com/forum/member.php?53413-ARMaterials)



What I did use and what worked nicely I received from Greg Maxey. As follows:


Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim lngIndex As Long
Dim arrWords() As String
Dim arrRandom
arrWords = Split(Left(Selection.Range.Text, Len(Selection.Range.Text) - 1), vbCr)
arrRandom = RandomizeArray(arrWords)
Selection.Delete
For lngIndex = 0 To UBound(arrRandom)
Selection.Range.Text = Selection.Range.Text & arrRandom(lngIndex) & vbCr
Next lngIndex
End Sub


Function RandomizeArray(arrInput() As String) As Variant
Dim lngIndex As Long
Dim varTemp As Variant
Dim lngRandom As Long
Dim varRandomized As Variant

Randomize
ReDim varRandomized(LBound(arrInput) To UBound(arrInput))
For lngIndex = LBound(arrInput) To UBound(arrInput)
varRandomized(lngIndex) = arrInput(lngIndex)
Next lngIndex
For lngIndex = LBound(arrInput) To UBound(arrInput)
lngRandom = CLng(((UBound(varRandomized) - lngIndex) * Rnd) + lngIndex)
varTemp = varRandomized(lngIndex)
varRandomized(lngIndex) = varRandomized(lngRandom)
varRandomized(lngRandom) = varTemp
Next lngIndex
RandomizeArray = varRandomized
End Function

SamT
04-28-2014, 12:57 PM
snb (http://www.vbaexpress.com/forum/member.php?44644-snb)

This is what I posted:



Sub M_randomise_words_snb()
Randomize

sn = Split(Trim(Replace(ActiveDocument.Content, vbCr, " ")))
sp = sn

For j = 0 To UBound(sp) - 1
sp(j) = sn(Rnd * (UBound(sn)))
sn = Split(Trim(Replace(Join(sn) & " ", sp(j) & " ", "", , 1)))
Next
sp(j) = sn(0)

Documents.Add.Content = Join(sp, vbCr)
End Sub