Results 1 to 5 of 5

Thread: Change listbox value

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #4
    Administrator Excel Fox's Avatar
    Join Date
    Mar 2011
    Posts
    1,401
    Rep Power
    10
    This should help. I've added textboxes to a second userform during run time when you click on one row in the first userform.

    Here's the code for reference

    Code:
    Public var As Variant
    Dim rng As Range
    Private Sub ListBox1_Click()
    
        UserForm2.Show 0
        
    End Sub
    
    Private Sub UserForm_Initialize()
    
        With ListBox1
            Set rng = Worksheets(Replace(Split(.RowSource, "!")(0), "'", "")).Range(Split(.RowSource, "!")(1))
    'If your row source doesn't have reference to a sheet, but only to a range, just use Set rng = Range(RowSource)
        var = rng
            .RowSource = ""
            .List = var
        End With
        
    End Sub
    
    Private Sub UserForm_Terminate()
    
        rng.Value = var
        
    End Sub
    Code:
    Dim txtSampleTextBox() As MSForms.TextBox
    Private Sub UserForm_Initialize()
        
        Dim lng As Long
        Dim obj As MSForms.TextBox
        lng = UserForm1.ListBox1.ColumnCount
        ReDim Preserve txtSampleTextBox(1 To lng)
        
        For lng = 1 To lng
            Set txtSampleTextBox(lng) = Controls.Add("Forms.TextBox.1", "txtSampleTextBox" & lng)
            With txtSampleTextBox(lng)
                .Left = (lng - 1) * .Width + 5
                .Text = UserForm1.ListBox1.List(UserForm1.ListBox1.ListIndex, lng - 1)
            End With
        Next lng
        Me.Width = (lng - 1) * (txtSampleTextBox(lng - 1).Width + 1)
    
    End Sub
    
    Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
    
        Dim lng As Long
        With UserForm1.ListBox1
            For lng = 1 To Me.Controls.Count
                .List(.ListIndex, lng - 1) = Controls("txtSampleTextBox" & lng).Text
            Next lng
            UserForm1.var = .List
        End With
        
    End Sub
    Attached Files Attached Files
    A dream is not something you see when you are asleep, but something you strive for when you are awake.

    It's usually a bad idea to say that something can't be done.

    The difference between dream and aim, is that one requires soundless sleep to see and the other requires sleepless efforts to achieve

    Join us at Facebook

Similar Threads

  1. Maximum columns in a ListBox
    By Rasm in forum Excel Help
    Replies: 7
    Last Post: 05-04-2020, 12:44 PM
  2. How To Create DropDown ListBox In Excel
    By lokvan in forum Excel Help
    Replies: 3
    Last Post: 12-23-2014, 04:43 PM
  3. Replies: 8
    Last Post: 04-16-2013, 02:04 PM
  4. Change Display Range Based On Change of Dropdown Values
    By rich_cirillo in forum Excel Help
    Replies: 2
    Last Post: 03-29-2013, 04:58 AM
  5. Using ListView - a Listbox on steroids
    By Rasm in forum Download Center
    Replies: 3
    Last Post: 04-09-2011, 03:34 AM

Posting Permissions

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