+ Reply to Thread
Results 1 to 4 of 4

Populate userform from row values based on combobox

Hybrid View

  1. #1
    Registered User
    Join Date
    07-19-2014
    Location
    uk
    MS-Off Ver
    2010
    Posts
    3

    Populate userform from row values based on combobox

    hi,

    how can I build a userform to display all values from a row? in order to select a row, i was thinking to use a combobox to display values from one column and based on that selection, display all values in that row? the reason i want to use a userform is because i would also like to be able to edit that data. Any help would be much appreciated!!!!

    thanks.

  2. #2
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: Populate userform from row values based on combobox

    So you have a UserForm, lets call it UserForm1, and in it you have a combobox, lets call it combobox1, which will display all values from a column, in our case we will assume column A. You want to select a value in combobox1 and have it display all the values in that row in a listbox, lets call it listbox1. All of this will be occuring based upon the values in a worksheet which we will call "Sheet1". Here is code:

    Private Sub ComboBox1_Change()
    Dim ws As Worksheet:    Set ws = Sheets("Sheet1")
    Dim rFind As Range, rCol As Range
    Dim LC As Long
    
    Set rFind = ws.Range("A1:A" & ws.Range("A" & Rows.Count).End(xlUp).Row).Find(What:=ComboBox1.Value, LookIn:=xlValues, LookAt:=xlWhole)
    
    If Not rFind Is Nothing Then
        LC = ws.Cells(rFind.Row, Columns.Count).End(xlToLeft).Column
        For Each rCol In ws.Range(ws.Cells(rFind.Row, 2), ws.Cells(rFind.Row, LC))
            If Not IsEmpty(rCol) Then
                With ListBox1
                    .AddItem rCol.Value
                End With
            End If
        Next rCol
    End If
    
    End Sub
    
    Private Sub UserForm_Initialize()
    Dim ws As Worksheet:    Set ws = Sheets("Sheet1")
    Dim rCell As Range
    
    If ws.Range("A" & Rows.Count).End(xlUp).Row = 1 And ws.Range("A1").Value = "" Then
        MsgBox ("There is no data to populate combobox1.")
        Exit Sub
    End If
    
    For Each rCell In ws.Range("A1:A" & ws.Range("A" & Rows.Count).End(xlUp).Row)
        If Not IsEmpty(rCell) Then
            With ComboBox1
                .AddItem rCell.Value
            End With
        End If
    Next rCell
    
    End Sub

  3. #3
    Registered User
    Join Date
    07-19-2014
    Location
    uk
    MS-Off Ver
    2010
    Posts
    3

    Re: Populate userform from row values based on combobox

    Thank you so much!

    If I wanted to have each value displayed on a texbox, how would I have to do it?

  4. #4
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Populate userform from row values based on combobox

    This uses a multi column listbox rather than VLookup. As written, it only deals with 3 columns of data. If you need more you should change the RangeOfData function, and add more TextBoxes
    Dim ufEventsDisabled As Boolean
    
    Private Sub ListBox1_Click()
        If ufEventsDisabled Then Exit Sub
        With ListBox1
            If .ListIndex <> -1 Then
                ufEventsDisabled = True
                Me.TextBox1 = .List(.ListIndex, 0)
                Me.TextBox2 = .List(.ListIndex, 1)
                Me.TextBox3 = .List(.ListIndex, 2)
                Me.CommandButton2.Enabled = False
                ufEventsDisabled = False
            End If
        End With
    End Sub
    
    Private Sub CommandButton2_Click()
        Rem apply changes
        If ufEventsDisabled Then Exit Sub
        With Me.ListBox1
            If .ListIndex <> -1 Then
                ufEventsDisabled = True
                .List(.ListIndex, 0) = Me.TextBox1.Text
                .List(.ListIndex, 1) = Me.TextBox2.Text
                .List(.ListIndex, 2) = Me.TextBox3.Text
                ufEventsDisabled = False
                RangeOfData.Cells(.ListIndex + 1, 1) = Me.TextBox1.Text
                RangeOfData.Cells(.ListIndex + 1, 2) = Me.TextBox2.Text
                RangeOfData.Cells(.ListIndex + 1, 3) = Me.TextBox3.Text
                Me.CommandButton2.Enabled = False
            End If
        End With
    End Sub
    
    Private Sub UserForm_Initialize()
        With Me.ListBox1
            .ColumnCount = RangeOfData.Columns.Count
            .List = RangeOfData.Value
            .ColumnWidths = Application.WorksheetFunction.Rept(";0", .ColumnCount)
        End With
        Me.CommandButton2.Enabled = False
    End Sub
    
    Function RangeOfData() As Range
        With Sheet1.Range("A:A")
            Set RangeOfData = Range(.Cells(2, 3), .Cells(Rows.Count, 1).End(xlUp))
        End With
    End Function
    
    Private Sub TextBox1_Change()
        If ufEventsDisabled Then Exit Sub
        Me.CommandButton2.Enabled = True
    End Sub
    Private Sub TextBox2_Change()
        If ufEventsDisabled Then Exit Sub
        Me.CommandButton2.Enabled = True
    End Sub
    Private Sub TextBox3_Change()
        If ufEventsDisabled Then Exit Sub
        Me.CommandButton2.Enabled = True
    End Sub
    
    Private Sub CommandButton1_Click()
        Unload Me
    End Sub
    Attached Files Attached Files
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Trying to populate a userform combobox from a userform textbox
    By jtemp57 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-31-2014, 03:33 PM
  2. Userform Combobox to populate based on selection in another combobox
    By Excelnoub in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-14-2014, 03:34 PM
  3. [SOLVED] Userform combobox to populate RowSource for new values in combobox
    By Stratfordoaks in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 12-17-2013, 01:18 PM
  4. populate a combobox based on unique values in another workbook
    By roninn75 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-03-2013, 03:36 AM
  5. [SOLVED] Excel Userform: Populate other controls (i.e. textbox & combobox) based on combobox select
    By MileHigh_PhD in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 01-22-2013, 04:50 PM

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.6.0 RC 1