Help Creating a User form

NatetheGreat

Active Member
Joined
Nov 18, 2013
Messages
268
Hello,
I am totally new to creating userforms, and have done a bit of googling but either find information too basic of too confusing.

I have inserted and formatted a button into a new userform in my workbook "Userform1". I have then placed two text boxes, two labels, and one command button. I want within my code, for the form to open, and the values the user puts in either box to be passed into the module.

So in module 1

sub somecode()

code
code
code
userform1.show
storedval=userform1.textbox1.value
soredval2=userform1.textbox2.value

end sub

So I want the userform to open, user to put in some values, click the command button, the form to close and the values to pass from the textboxes into the sub somecode()
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.

ranman256

Well-known Member
Joined
Jun 17, 2014
Messages
2,235
Rather than building forms and boxes and such, cant you do the same thing with a worksheet?
Paint the cells blue, except for 2 cells (your 2 'boxes') and a button to run the macro, that reads the cells:
storeDval = range("B2").value
storeDval2 = range("B3").value

less code, less headaches.
 
Upvote 0

NatetheGreat

Active Member
Joined
Nov 18, 2013
Messages
268
Sorry I should have probably mentioned that the above example is just for me to try and understand how it works, the code I will put it into is much more demanding hence the need for the userforms.
 
Upvote 0

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
85,127
Office Version
  1. 365
Platform
  1. Windows
click the command button, the form to close
Rather than closing the form you could hide it
Code:
Private Sub DoneBtn_Click()

    Me.Hide

End Sub
& then close it at the end of the sub.
Code:
    Unload TitleFrm
Exit Sub
This is how I do it
 
Upvote 0

hateme28

Board Regular
Joined
Jul 2, 2014
Messages
161
ADVERTISEMENT
I think this is what you're looking for...

Code:
So in module 1

public storedval as string
public soredval2 as string
public sub somecode()

code
code
code
userform1.show
storedval=userform1.textbox1.value
soredval2=userform1.textbox2.value

end sub

userform

Code:
private sub commandbutton1_click()
userform1.hide

end sub

or
Code:
private sub commandbutton1_click()

storedval = textbox1.value
soredval2 = textbox2.value
unload userform1

end sub
 
Upvote 0

NatetheGreat

Active Member
Joined
Nov 18, 2013
Messages
268
I think this is what you're looking for...

Code:
So in module 1

public storedval as string
public soredval2 as string
public sub somecode()

code
code
code
userform1.show
storedval=userform1.textbox1.value
soredval2=userform1.textbox2.value

end sub

userform

Code:
private sub commandbutton1_click()
userform1.hide

end sub

or
Code:
private sub commandbutton1_click()

storedval = textbox1.value
soredval2 = textbox2.value
unload userform1

end sub

Hi,

Problem is still that the variable doesn't seem to be passing from the userform into module 1. Currently I have the following example

Code:
Public Name as String
Public Sub Search
code
code
code
userform1.show
name =  userform1.textbox1.value
end sub

the userform1 has the code

Code:
Private Sub CommandButton1_Click()
name = TextBox1.Value
storedval2 = TextBox2.Value
Unload UserForm1
End Sub

When stepping from the userform back into the search sub, the name variable never gets populated with what was entered into the userform box!
 
Last edited:
Upvote 0

Forum statistics

Threads
1,195,834
Messages
6,011,866
Members
441,651
Latest member
drewe2000

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
Top