PDA

View Full Version : Picking a random name



Waubain
07-07-2010, 06:26 AM
I want to pick a name (Bob, Ted, Carol, or Alice) randomly from an array and display it in a message box. I would like to do this from a command button on a PP slide. I can sort or search an array, but I cannot figure how to randomly pick one of the names.

mdmackillop
07-07-2010, 09:35 AM
Sub Rndm()
Dim arr
arr = Array("Bob", "Ted", "Carol", "Alice")
MsgBox arr(Int((4 * Rnd)))
End Sub

Waubain
07-07-2010, 10:10 AM
Thank you. This worked very well. I have 108 students in my class, but only need to propagate the array once per year.

mdmackillop
07-07-2010, 11:21 AM
This will work with a text (txt) file, one name per line, you can use First and Surnames, or initials
Ted Smith
A Brown
etc.


Sub Rndm()
Dim arr, fs, a

Set fs = CreateObject("Scripting.FileSystemObject")
Set a = fs.openTextFile("c:\AA\Names.txt")
arr = Split(a.readall, Chr(13) & Chr(10))
MsgBox arr(Int((4 * Rnd)))
End Sub