Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 26
Default Label with randomly selected text from cells

Hello everybody,

I was given the following code yesterday by Tom Ogilvy to select a text
randomly from a list of cells in a message box.

Sub Text()

Set rng = Sheets("sheet2").Range("A1:A5")
i = Int(Rnd() * rng.Count + 1)
MsgBox rng(i)

End Sub


The question is how can I do the same for the text of a label in a userform?
Thank you for your help.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Label with randomly selected text from cells

Private Sub Userform_Initialize()

Dim rng as Range, i as Long
Set rng = Sheets("sheet2").Range("A1:A5")
i = Int(Rnd() * rng.Count + 1)
me.Label1.Caption = rng(i).Value

End Sub


this should be in the userform module.

--
Regards,
Tom Ogilvy


"Cajeto 63" wrote in message
...
Hello everybody,

I was given the following code yesterday by Tom Ogilvy to select a text
randomly from a list of cells in a message box.

Sub Text()

Set rng = Sheets("sheet2").Range("A1:A5")
i = Int(Rnd() * rng.Count + 1)
MsgBox rng(i)

End Sub


The question is how can I do the same for the text of a label in a
userform?
Thank you for your help.



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 26
Default Label with randomly selected text from cells

Thank you very much Tom,
Once again it works perfectly.

Thanks for your help.


"Tom Ogilvy" wrote:

Private Sub Userform_Initialize()

Dim rng as Range, i as Long
Set rng = Sheets("sheet2").Range("A1:A5")
i = Int(Rnd() * rng.Count + 1)
me.Label1.Caption = rng(i).Value

End Sub


this should be in the userform module.

--
Regards,
Tom Ogilvy


"Cajeto 63" wrote in message
...
Hello everybody,

I was given the following code yesterday by Tom Ogilvy to select a text
randomly from a list of cells in a message box.

Sub Text()

Set rng = Sheets("sheet2").Range("A1:A5")
i = Int(Rnd() * rng.Count + 1)
MsgBox rng(i)

End Sub


The question is how can I do the same for the text of a label in a
userform?
Thank you for your help.




  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 26
Default Label with randomly selected text from cells

Tom,
I added this code in my folder but it doesn't really work.
The strange thing is that when I run the macro several times in a row with
the file open it works perfectly. But if I Close/Open Close/Open Close/Open,
each time I open it gives the same message. Any idea on how to change this?

By the way, is it possible to change the color of a msgbox?

Thank you.


"Cajeto 63" wrote:

Thank you very much Tom,
Once again it works perfectly.

Thanks for your help.


"Tom Ogilvy" wrote:

Private Sub Userform_Initialize()

Dim rng as Range, i as Long
Set rng = Sheets("sheet2").Range("A1:A5")
i = Int(Rnd() * rng.Count + 1)
me.Label1.Caption = rng(i).Value

End Sub


this should be in the userform module.

--
Regards,
Tom Ogilvy


"Cajeto 63" wrote in message
...
Hello everybody,

I was given the following code yesterday by Tom Ogilvy to select a text
randomly from a list of cells in a message box.

Sub Text()

Set rng = Sheets("sheet2").Range("A1:A5")
i = Int(Rnd() * rng.Count + 1)
MsgBox rng(i)

End Sub


The question is how can I do the same for the text of a label in a
userform?
Thank you for your help.




  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Label with randomly selected text from cells

Private Sub Userform_Initialize()

Dim rng as Range, i as Long
randomize
Set rng = Sheets("sheet2").Range("A1:A5")
i = Int(Rnd() * rng.Count + 1)
me.Label1.Caption = rng(i).Value

End Sub


--
Regards,
Tom Ogilvy



"Cajeto 63" wrote in message
...
Tom,
I added this code in my folder but it doesn't really work.
The strange thing is that when I run the macro several times in a row with
the file open it works perfectly. But if I Close/Open Close/Open
Close/Open,
each time I open it gives the same message. Any idea on how to change
this?

By the way, is it possible to change the color of a msgbox?

Thank you.


"Cajeto 63" wrote:

Thank you very much Tom,
Once again it works perfectly.

Thanks for your help.


"Tom Ogilvy" wrote:

Private Sub Userform_Initialize()

Dim rng as Range, i as Long
Set rng = Sheets("sheet2").Range("A1:A5")
i = Int(Rnd() * rng.Count + 1)
me.Label1.Caption = rng(i).Value

End Sub


this should be in the userform module.

--
Regards,
Tom Ogilvy


"Cajeto 63" wrote in message
...
Hello everybody,

I was given the following code yesterday by Tom Ogilvy to select a
text
randomly from a list of cells in a message box.

Sub Text()

Set rng = Sheets("sheet2").Range("A1:A5")
i = Int(Rnd() * rng.Count + 1)
MsgBox rng(i)

End Sub


The question is how can I do the same for the text of a label in a
userform?
Thank you for your help.





Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
what can I do to randomly select cells that contain text? gmadden2 Excel Worksheet Functions 2 January 6th 09 05:58 PM
Randomly populating empty cells with other text cells Throme88 Excel Discussion (Misc queries) 3 July 1st 08 02:58 PM
Selecting the value from a randomly selected cell out of a range Steve W. Excel Discussion (Misc queries) 1 June 3rd 08 06:27 PM
how to sort a column of Randomly selected numbers? Khoshravan Excel Worksheet Functions 8 July 14th 07 08:16 PM
How to generate a list of randomly selected numbers within a range Bob Excel Worksheet Functions 2 November 7th 05 06:09 PM


All times are GMT +1. The time now is 04:44 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"