PC Review


Reply
Thread Tools Rate Thread

Arbitrary data to Textboxes in UserForm

 
 
Rock
Guest
Posts: n/a
 
      5th May 2010
I created a UserForm named UserForm2 with 4 Textboxes named Textbox1,
Textbox2, TextBox3, and Textbox4. I want to call UserForm2 (UserForm2.Show)
and have the following procedure execute.

-TextBox1 populate with random data from one cell in Sheet1 range B1:B200

-TextBox2, TextBox3, TextBox4 each populate with data from one different
random cell Sheet2 range B1:B200

I can insert this code below into TextBox1 of UserForm2 and it will pull
random data from one cell in Sheet1 range B1:B200 when I call the form

Private Sub UserForm_Initialize()
TextBox1.Text = Range("B" & Int(123 * Rnd) + 2).Value
End Sub

However if activate Sheet2 (Sheet2.Activate) and try to use the same code
for TextBox2, TextBox3, and TextBox4, I get no result; the textboxes remain
blank. Can you help me understand why my procedure is not working and how I
can get the result I am looking for with TextBox2, TextBox3, and TextBox4?

 
Reply With Quote
 
 
 
 
JLGWhiz
Guest
Posts: n/a
 
      5th May 2010
TextBox1.Text = Range("B" & Int(123 * Rnd) + 2).Value

I assume you are changing the TextBox name. You also need to Qualify your
range reference with the applicable sheet object, i.e.

TextBox2.Text = Sheets("Sheet2").Range("B" & Int(123*Rnd + 2).Value

Otherwise, it reverts to the active sheet.




"Rock" <(E-Mail Removed)> wrote in message
news:BE7121E9-A5C5-46BB-AD72-(E-Mail Removed)...
>I created a UserForm named UserForm2 with 4 Textboxes named Textbox1,
> Textbox2, TextBox3, and Textbox4. I want to call UserForm2
> (UserForm2.Show)
> and have the following procedure execute.
>
> -TextBox1 populate with random data from one cell in Sheet1 range B1:B200
>
> -TextBox2, TextBox3, TextBox4 each populate with data from one different
> random cell Sheet2 range B1:B200
>
> I can insert this code below into TextBox1 of UserForm2 and it will pull
> random data from one cell in Sheet1 range B1:B200 when I call the form
>
> Private Sub UserForm_Initialize()
> TextBox1.Text = Range("B" & Int(123 * Rnd) + 2).Value
> End Sub
>
> However if activate Sheet2 (Sheet2.Activate) and try to use the same code
> for TextBox2, TextBox3, and TextBox4, I get no result; the textboxes
> remain
> blank. Can you help me understand why my procedure is not working and how
> I
> can get the result I am looking for with TextBox2, TextBox3, and TextBox4?
>



 
Reply With Quote
 
Dave Peterson
Guest
Posts: n/a
 
      5th May 2010
Try specifying the sheet that contains that range:

Private Sub UserForm_Initialize()
TextBox1.Text = worksheets("Sheet1").Range("B" & Int(123 * Rnd) + 2).Value
TextBox2.Text = worksheets("Sheet2").Range("B" & Int(123 * Rnd) + 2).Value
TextBox3.Text = worksheets("Sheet2").Range("B" & Int(123 * Rnd) + 2).Value
TextBox4.Text = worksheets("Sheet2").Range("B" & Int(123 * Rnd) + 2).Value
End Sub

Rock wrote:
>
> I created a UserForm named UserForm2 with 4 Textboxes named Textbox1,
> Textbox2, TextBox3, and Textbox4. I want to call UserForm2 (UserForm2.Show)
> and have the following procedure execute.
>
> -TextBox1 populate with random data from one cell in Sheet1 range B1:B200
>
> -TextBox2, TextBox3, TextBox4 each populate with data from one different
> random cell Sheet2 range B1:B200
>
> I can insert this code below into TextBox1 of UserForm2 and it will pull
> random data from one cell in Sheet1 range B1:B200 when I call the form
>
> Private Sub UserForm_Initialize()
> TextBox1.Text = Range("B" & Int(123 * Rnd) + 2).Value
> End Sub
>
> However if activate Sheet2 (Sheet2.Activate) and try to use the same code
> for TextBox2, TextBox3, and TextBox4, I get no result; the textboxes remain
> blank. Can you help me understand why my procedure is not working and how I
> can get the result I am looking for with TextBox2, TextBox3, and TextBox4?


--

Dave Peterson
 
Reply With Quote
 
Rock
Guest
Posts: n/a
 
      7th May 2010
Got it. Thank you JLGWhiz!

"JLGWhiz" wrote:

> TextBox1.Text = Range("B" & Int(123 * Rnd) + 2).Value
>
> I assume you are changing the TextBox name. You also need to Qualify your
> range reference with the applicable sheet object, i.e.
>
> TextBox2.Text = Sheets("Sheet2").Range("B" & Int(123*Rnd + 2).Value
>
> Otherwise, it reverts to the active sheet.
>
>
>
>
> "Rock" <(E-Mail Removed)> wrote in message
> news:BE7121E9-A5C5-46BB-AD72-(E-Mail Removed)...
> >I created a UserForm named UserForm2 with 4 Textboxes named Textbox1,
> > Textbox2, TextBox3, and Textbox4. I want to call UserForm2
> > (UserForm2.Show)
> > and have the following procedure execute.
> >
> > -TextBox1 populate with random data from one cell in Sheet1 range B1:B200
> >
> > -TextBox2, TextBox3, TextBox4 each populate with data from one different
> > random cell Sheet2 range B1:B200
> >
> > I can insert this code below into TextBox1 of UserForm2 and it will pull
> > random data from one cell in Sheet1 range B1:B200 when I call the form
> >
> > Private Sub UserForm_Initialize()
> > TextBox1.Text = Range("B" & Int(123 * Rnd) + 2).Value
> > End Sub
> >
> > However if activate Sheet2 (Sheet2.Activate) and try to use the same code
> > for TextBox2, TextBox3, and TextBox4, I get no result; the textboxes
> > remain
> > blank. Can you help me understand why my procedure is not working and how
> > I
> > can get the result I am looking for with TextBox2, TextBox3, and TextBox4?
> >

>
>
> .
>

 
Reply With Quote
 
Rock
Guest
Posts: n/a
 
      7th May 2010
This is very helpful Dave. Thank you!

"Dave Peterson" wrote:

> Try specifying the sheet that contains that range:
>
> Private Sub UserForm_Initialize()
> TextBox1.Text = worksheets("Sheet1").Range("B" & Int(123 * Rnd) + 2).Value
> TextBox2.Text = worksheets("Sheet2").Range("B" & Int(123 * Rnd) + 2).Value
> TextBox3.Text = worksheets("Sheet2").Range("B" & Int(123 * Rnd) + 2).Value
> TextBox4.Text = worksheets("Sheet2").Range("B" & Int(123 * Rnd) + 2).Value
> End Sub
>
> Rock wrote:
> >
> > I created a UserForm named UserForm2 with 4 Textboxes named Textbox1,
> > Textbox2, TextBox3, and Textbox4. I want to call UserForm2 (UserForm2.Show)
> > and have the following procedure execute.
> >
> > -TextBox1 populate with random data from one cell in Sheet1 range B1:B200
> >
> > -TextBox2, TextBox3, TextBox4 each populate with data from one different
> > random cell Sheet2 range B1:B200
> >
> > I can insert this code below into TextBox1 of UserForm2 and it will pull
> > random data from one cell in Sheet1 range B1:B200 when I call the form
> >
> > Private Sub UserForm_Initialize()
> > TextBox1.Text = Range("B" & Int(123 * Rnd) + 2).Value
> > End Sub
> >
> > However if activate Sheet2 (Sheet2.Activate) and try to use the same code
> > for TextBox2, TextBox3, and TextBox4, I get no result; the textboxes remain
> > blank. Can you help me understand why my procedure is not working and how I
> > can get the result I am looking for with TextBox2, TextBox3, and TextBox4?

>
> --
>
> Dave Peterson
> .
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Send and Receive Data From Web Site Into UserForm TextBoxes Minitman Microsoft Excel Misc 0 4th Aug 2008 05:33 PM
Update Data On Sheet From UserForm TextBoxes Minitman Microsoft Excel Programming 3 30th Jul 2007 02:34 PM
Need help w/ userform with 12 textboxes with data going into colum =?Utf-8?B?Y2oyazJr?= Microsoft Excel Misc 1 13th Jun 2007 02:38 PM
need to add textboxes to userform on the fly Stuart Holley Microsoft Excel Programming 7 29th Mar 2004 12:23 AM
userform textboxes Jo Microsoft Excel Programming 4 21st Oct 2003 07:15 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:49 PM.