ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Random Text in a userform textbox (https://www.excelbanter.com/excel-programming/352853-random-text-userform-textbox.html)

Soniya[_4_]

Random Text in a userform textbox
 
Hi All,

Is it possible to display random text from a range of cells in a
userform textbox when the userform is activated?

Also if the range refers to non contiguous cells.

Thanks


Toppers

Random Text in a userform textbox
 
Hi,
Something like this? Put in "Userform_Initialize" routine

Set rng = Range("A1:B1,B3,C5,B8,C10")
mystr = ""
For Each cell In rng
mystr = mystr + cell.Value & " "
Next
textbox1.Text = mystr

"Soniya" wrote:

Hi All,

Is it possible to display random text from a range of cells in a
userform textbox when the userform is activated?

Also if the range refers to non contiguous cells.

Thanks



Tom Ogilvy

Random Text in a userform textbox
 
If you only want the text from one cell on a random selection then
To randomize you could do:

Dim rng as Range, i as Long, j as Long
Dim cell as Range
Set rng = Range("A1:B1,B3,C5,B8,C10")
i = int(rnd()*rng.count+1)
j = 0
For Each cell In rng
j = j +1
if j = i then
TextBox1.Text = cell.Value
exit for
End if
Next

--
Regards,
Tom Ogilvy

"Toppers" wrote in message
...
Hi,
Something like this? Put in "Userform_Initialize" routine

Set rng = Range("A1:B1,B3,C5,B8,C10")
mystr = ""
For Each cell In rng
mystr = mystr + cell.Value & " "
Next
textbox1.Text = mystr

"Soniya" wrote:

Hi All,

Is it possible to display random text from a range of cells in a
userform textbox when the userform is activated?

Also if the range refers to non contiguous cells.

Thanks






All times are GMT +1. The time now is 01:58 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com