Populating Textboxes in a Userform

TheKernz

New Member
Joined
May 13, 2014
Messages
11
Hey everyone,
I'm trying to figure out how to open a userform with textboxes already filled in based on values in a cell range. I can't seem to figure this out for the life of me.
Thanks
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Paste the code in the form's code so it can run on the event when the form is initialized (right-click on the form object and select "View Code").

Code:
Private Sub UserForm_Initialize()

    TextBox1 = "I'm filled in"
    TextBox2 = "I'm also filled in "

End Sub
 
Upvote 0
My problem is that the textboxes change which cells need to be referenced depending on which command button in my sheet has been pressed. Is there anyway to write the userfrom_initialize in another sub?
Thanks
 
Upvote 0
There are a variety of event handling procedures. What you want is the Button_Click event.

Code:
Private Sub CommandButton1_Click() [COLOR="#008000"]' This runs when "Button1" is clicked[/COLOR]
    TextBox1 = Range("A1")
    TextBox2 = Range("A2")
End Sub

Private Sub CommandButton2_Click() [COLOR="#008000"]' This runs when "Button2" is clicked[/COLOR]
    TextBox1 = Range("B1")
    TextBox2 = Range("B2")
End Sub
 
Upvote 0
There are a variety of event handling procedures. What you want is the Button_Click event.

Code:
Private Sub CommandButton1_Click() [COLOR=#008000]' This runs when "Button1" is clicked[/COLOR]
    TextBox1 = Range("A1")
    TextBox2 = Range("A2")
End Sub

Private Sub CommandButton2_Click() [COLOR=#008000]' This runs when "Button2" is clicked[/COLOR]
    TextBox1 = Range("B1")
    TextBox2 = Range("B2")
End Sub

That's what my code originally was, however it does not populate the textboxes. Any other suggestions? To me I think I need something that initializes the userform. I tried putting the above code in with a userform.show but to no avail. An in code userform_initialize to me seems the way to do it, only I don't know if such a piece of code exists.
 
Upvote 0
The code I provided in Post #4 should populate the textboxes with the values in cells A1 & A2 or B1 & B2 when you press the buttons. It will work if all of these conditions are met:

  1. You have two buttons on your form named "Button1" and "Button2"
  2. You have two textboxes named "TextBox1" and "TextBox2"
  3. The code is pasted in the form's code window
 
Upvote 0

Forum statistics

Threads
1,215,425
Messages
6,124,824
Members
449,190
Latest member
rscraig11

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top