Log in

View Full Version : How To Close A UserForm With A CommandButton



jeff
07-20-2013, 11:43 AM
OK I am having my first attempt with creating a Userform.I am reading up on how to create.
1. How do I add entries into the dropdown box`s on the userform
2. How to I get the userform to open when I press the command button.I tried with a macro recorder but I did not get it to open successfully
I will keep reading and hopefully get some answers
Jeff

jeff
07-20-2013, 02:05 PM
Ok
I got a small code to open the userform.This seems to work but the code I have used to close the userform gives error.Why does it give error and am I on the correct path so far?
Jeff

Excel Fox
07-20-2013, 02:45 PM
The code to unload from within a routine in the user-form is not Unload.Me, it's
Unload Me

jeff
07-20-2013, 07:52 PM
How do I make entries in the user forms drop down boxs

jeff
07-21-2013, 04:53 AM
OK....I have added a list of entries into the first combobox.....Is this the correct way to add entries into a combobox..if yes,iwill proceed with the rest
Jeff

Kenneth Hobson
07-21-2013, 07:14 AM
A named Range for the ListRow works fine. You can also use ComboBox1.List = somearray() or add one item at a time with ComboBox1.AddItem method.

If you are going to use a Range name, add a dynamic range or reset the named range in the Userform's Initialize event.

jeff
07-21-2013, 07:45 AM
Thank you Kenneth.Would you have a sample file of your mentioned methods.This would be appreciated.Jeff

Kenneth Hobson
07-21-2013, 08:41 AM
You only need one way to solve a problem. In any case, this should show you some other methods.

Notice that I added ken as the first item after I added other items. I then added the last 3 items from the named range. That method can be used to get any range in a dynamic way.

While adding the 3 items created duplicates, to remove them, is another issue. It is a common question. If you need to do that, search this forum or the web or start a new thread. There again, there are several ways to solve the problem.


Private Sub UserForm_Initialize()
Dim c As Range, r As Range
With ComboBox1
.RowSource = vbNullString
'.RowSource = "Machines"
.List = Range("Machines").Value
.AddItem "Ken", 0

Set r = Worksheets("Lists").Range("A7", _
Worksheets("Lists").Range("A" & Rows.Count).End(xlUp))
For Each c In r
.AddItem c.Value, -1
Next c
End With
End Sub

jeff
07-21-2013, 12:37 PM
Thank you

Could you please explain - If you are going to use a Range name, add a dynamic range or reset the named range in the Userform's Initialize event.

Jeff