Hi. This macro, when run aftter startup, opens a text file, populates a listbox, reads a random entry from the listbox and creates a message box. It works fine after startup, but at startup the messagebox is always the last line from the text file / listbox. Can someone please tell me why it does this and what, if anything, can be done to fix it. thanks Graeme Public Sub AcadStartup() Dim TextLine Dim j As Integer Dim randnum As Integer Open "c:\ACADmessages.txt" For Input As #1 Do While Not EOF(1) Line Input #1, TextLine UserForm2.ListBox1.AddItem (TextLine) Loop Close #1 j = UserForm2.ListBox1.ListCount randnum = Int((j * Rnd) + 1) MsgBox UserForm2.ListBox1.List(randum) End Sub
See comments in-line Since ListBox's ListIndex is from 0 to ListCount-1, so this line of code shuld be: randnum=Int((j*Rnd) 'randnum will get value from 0 to j-1 Do you get run-time error here: "randum" is undecleared variable here? Or it is just a typo? you could test to see whether you get "randnum" value correctly: MsgBox "Random Value=" & randnum & vbcr & UserForm2.ListBox1.List(randnum)
Also.... to keep it from running the same pattern of "random" numbers each time you need to add the following to your code: Private Sub UserForm_Click() Randomize Timer 'add this Dim TextLine ... ... James