+ Reply to Thread
Results 1 to 7 of 7

Thread: Userform

  1. #1
    Registered User
    Join Date
    12-08-2010
    Location
    Nottingham, England
    MS-Off Ver
    Excel 2003
    Posts
    15

    Userform

    Hi everyone, I've been given a task at work which needs to be done urgently so they've given it to me. I have very limited knowledge of VB programming so I'll need all the help I can get, especially with this subject.

    I need to create a userform that generates a new worksheet. I know how to physically create the userform, but where I struggle is:

    -Creating dropdown lists within some of the text fields on the userform
    -Programming the buttons

    The form looks something like this:

    Select Project: (This will be a manual input)
    Value: (Dropdown list with options of <250k and >250k)
    Plant: (Dropdown list)
    Region: (Dropdown list)
    Location: (Dropdown list)
    Engine Type: (Dropdown list)
    IPT Lead/Buyer: (manual input)

    Once the user inputs these they then press OK or DONE and ideally it should generate a worksheet for which I have a set template.

    If anyone could please just provide me with a push in the right direction or help me with what the outline of the code should look like, it will be massively appreciated. If I had more time I'd just do it myself via the help section on VB but I have a strict deadline to adhere to!

    Many thanks in advance!

  2. #2
    Forum Guru Bob Phillips's Avatar
    Join Date
    09-03-2005
    Location
    Wessex
    MS-Off Ver
    MS Excel 2010
    Posts
    2,248

    Re: Userform

    You don't create dropdown lists in a text fields, you use a different control, either a combobox or a listbox.

    These controls have events that you can use, such as

    Private Sub ComboBox1_Change()
        'do your stuff here
    End Sub

  3. #3
    Registered User
    Join Date
    06-06-2008
    Posts
    34

    Re: Userform

    As Bob said, create a combobox from the Toolbox, then to populate that combobox you could do the following:

    ComboBoxName.List = Array("NameX", "NameY", "NameZ")

    So just replcae ComboBoxName with the name you give your combobox, and then the NameX, NameY etc. with the values you want in the drop-down e.g. ">250k".

    For the manual inputs, just create a normal "textbox".

    Once this is all done, create a command button on your UserForm and go into the code view on that.

    Dim EmptyRow As Long
    Dim ws As Worksheet
    Set ws = Worksheets("YourSheetName")
    
    EmptyRow = ws.Cells(Rows.Count, 1) _
      .End(xlUp).Offset(1, 0).Row
    
    ws.Cells(EmptyRow, 1).Value = ComboBoxName.Value
    ws.Cells(EmptyRow, 2).Value = TextField1.Value
    ws.Cells(EmptyRow, 3).Value = TextField2.Value
    ws.Cells(EmptyRow, 4).Value = TextField3.Value
    ws.Cells(EmptyRow, 5).Value = TextField4.Value

    This basically adds the data from the userform into the empty rows on that spreadsheet template you've created. Let me know if you have any problems
    Last edited by Toddneyx; 12-09-2010 at 11:28 AM.

  4. #4
    Registered User
    Join Date
    12-08-2010
    Location
    Nottingham, England
    MS-Off Ver
    Excel 2003
    Posts
    15

    Re: Userform

    Many thanks for all your help, I will try it when I'm back in the office tomorrow morning and let you know how it goes. Thanks again, really appreciate it!

  5. #5
    Registered User
    Join Date
    12-08-2010
    Location
    Nottingham, England
    MS-Off Ver
    Excel 2003
    Posts
    15

    Re: Userform

    @Toddneyx: This is what I've done as an example:

    Private Sub ComboBox1_Change()
    ComboBoxName.List = Array("<250k", ">250k")
    End Sub
    But when I run this and the userform pops up, the drop down list for Value is blank, it doesn't have the two values from the code. Why is this?

  6. #6
    Registered User
    Join Date
    06-06-2008
    Posts
    34

    Re: Userform

    Ok firstly you have to change the "ComboBoxName" to the name of your combobox, which I'm guessing is called "ComboBox1"

    Then this code should be under userform_initialize()

    ComboBox1.List = Array("<250k", ">250k")

  7. #7
    Registered User
    Join Date
    06-06-2008
    Posts
    34

    Re: Userform

    Did you have any luck? Upload the spreadsheet if not

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.2.0