+ Reply to Thread
Results 1 to 46 of 46

Change listbox value

Hybrid View

  1. #1
    Registered User
    Join Date
    10-19-2012
    Location
    Assen
    MS-Off Ver
    Excel 2010
    Posts
    81

    Change listbox value

    Hello,

    I have created a sheet with a rowsource from A1:L19 in sheet1

    What i want to do is change the value's in a userform. I have an example of how i like to see it.

    http://www.excelforum.com/excel-prog...x-records.html

    But in that case i need to create for every cell a separate textbox. Is there a easier way to manage/change this? I only need to have the buttons edit and change. How can i manage this?
    See attached fileChange listbox.xlsm

  2. #2
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Change listbox value

    Hi Stingone

    This code is in the attached...let me know of issues.
    Option Explicit
    
    Private Sub UserForm_Initialize()
        Dim x As Long
        Dim k As Long
        Dim j As Long
        x = Sheets("Blad1").UsedRange.Rows.Count
        ListBox1.List = Sheets("Blad1").UsedRange.Value
    
        For k = 1 To x
            For j = 3 To 12
                ListBox1.List(k - 1, j - 1) = Format(ListBox1.List(k - 1, j - 1), "0.00")
            Next
        Next
    End Sub
    
    Private Sub CommandButton1_Click()
        Unload Me
    End Sub
    
    Private Sub CommandButton2_Click()
        Dim j As Long
        Dim x As Long
    
        x = Me.ListBox1.ListIndex + 1
        With Sheets("Blad1")
            For j = 1 To 12
                .Cells(x, j).Value = Me.Controls("TextBox" & j).Text
            Next j
        End With
    End Sub
    
    Private Sub ListBox1_Click()
        Dim k As Long
        Dim j As Long
        Dim Ctrl As Control
        For Each Ctrl In Me.Controls
            If TypeName(Ctrl) = "TextBox" Then
                k = Me.ListBox1.ListIndex
                '            Debug.Print Me.ListBox1.ListIndex
                For j = 1 To 12
                    Me.Controls("TextBox" & j).Text = Me.ListBox1.List(k, j - 1)
                Next j
            End If
        Next Ctrl
    End Sub
    Attached Files Attached Files
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please mark your Thread as SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

  3. #3
    Registered User
    Join Date
    10-19-2012
    Location
    Assen
    MS-Off Ver
    Excel 2010
    Posts
    81

    Re: Change listbox value

    Quote Originally Posted by jaslake View Post
    Hi Stingone

    This code is in the attached...let me know of issues.
    Option Explicit
    
    Private Sub UserForm_Initialize()
        Dim x As Long
        Dim k As Long
        Dim j As Long
        x = Sheets("Blad1").UsedRange.Rows.Count
        ListBox1.List = Sheets("Blad1").UsedRange.Value
    
        For k = 1 To x
            For j = 3 To 12
                ListBox1.List(k - 1, j - 1) = Format(ListBox1.List(k - 1, j - 1), "0.00")
            Next
        Next
    End Sub
    
    Private Sub CommandButton1_Click()
        Unload Me
    End Sub
    
    Private Sub CommandButton2_Click()
        Dim j As Long
        Dim x As Long
    
        x = Me.ListBox1.ListIndex + 1
        With Sheets("Blad1")
            For j = 1 To 12
                .Cells(x, j).Value = Me.Controls("TextBox" & j).Text
            Next j
        End With
    End Sub
    
    Private Sub ListBox1_Click()
        Dim k As Long
        Dim j As Long
        Dim Ctrl As Control
        For Each Ctrl In Me.Controls
            If TypeName(Ctrl) = "TextBox" Then
                k = Me.ListBox1.ListIndex
                '            Debug.Print Me.ListBox1.ListIndex
                For j = 1 To 12
                    Me.Controls("TextBox" & j).Text = Me.ListBox1.List(k, j - 1)
                Next j
            End If
        Next Ctrl
    End Sub
    When testing it gives an error "cant find library in Private Sub UserForm_Initialize()

  4. #4
    Registered User
    Join Date
    10-19-2012
    Location
    Assen
    MS-Off Ver
    Excel 2010
    Posts
    81

    Re: Change listbox value

    Error solved, problem with web components 11. The form is now showing but the values are not updated in the listbox when clicking on modify. The sheet update is working though
    Last edited by Stingone; 11-05-2012 at 04:12 PM.

  5. #5
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Change listbox value

    Hi Stingone

    Yes, the code as written will NOT update the ListBox...only the Sheet. Replace the code with this...it also updates the ListBox
    Option Explicit
    
    Private Sub UserForm_Initialize()
        Dim x As Long
        Dim k As Long
        Dim j As Long
        x = Sheets("Blad1").UsedRange.Rows.Count
        ListBox1.List = Sheets("Blad1").UsedRange.Value
    
        For k = 1 To x    'rows
            For j = 3 To 12    'columns
                ListBox1.List(k - 1, j - 1) = Format(ListBox1.List(k - 1, j - 1), "0.00")
            Next
        Next
    
        For j = 1 To 12
            Me.Controls("TextBox" & j).Text = ""
        Next j
        
        Me.ListBox1.ListIndex = -1
    End Sub
    
    Private Sub CommandButton1_Click()
        Unload Me
    End Sub
    
    Private Sub CommandButton2_Click()
        Dim j As Long
        Dim x As Long
    
        x = Me.ListBox1.ListIndex + 1    'row index
        With Sheets("Blad1")
            For j = 1 To 12    'columns
                .Cells(x, j).Value = Me.Controls("TextBox" & j).Text
            Next j
        End With
        Call UserForm_Initialize
    End Sub
    
    Private Sub ListBox1_Click()
        Dim k As Long
        Dim j As Long
        Dim Ctrl As Control
        For Each Ctrl In Me.Controls
            If TypeName(Ctrl) = "TextBox" Then
                k = Me.ListBox1.ListIndex    'row selected
                '            Debug.Print Me.ListBox1.ListIndex
                For j = 1 To 12
                    Me.Controls("TextBox" & j).Text = Me.ListBox1.List(k, j - 1)
                Next j
            End If
        Next Ctrl
    End Sub

  6. #6
    Registered User
    Join Date
    10-19-2012
    Location
    Assen
    MS-Off Ver
    Excel 2010
    Posts
    81

    Re: Change listbox value

    Works fine .. is there also a way that the textboxes can not be updated empty? when i now click on update with all field empty it gives an error

  7. #7
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Change listbox value

    Hi Stingone

    I'm missing something in your explanation. I can fill all the TextBoxes with blanks and it updates as expected and no error is reported.
    Please explain again.

  8. #8
    Registered User
    Join Date
    10-19-2012
    Location
    Assen
    MS-Off Ver
    Excel 2010
    Posts
    81

    Re: Change listbox value

    Quote Originally Posted by jaslake View Post
    Hi Stingone

    I'm missing something in your explanation. I can fill all the TextBoxes with blanks and it updates as expected and no error is reported.
    Please explain again.
    If you open the excel file and the userform disapears when clicking directly on the modify button without selecting a row it gives an 1004 error.

  9. #9
    Registered User
    Join Date
    10-19-2012
    Location
    Assen
    MS-Off Ver
    Excel 2010
    Posts
    81

    Re: Change listbox value

    Quote Originally Posted by jaslake View Post
    Hi Stignone

    See if this helps.
    
    
    Option Explicit
    
    Private Sub MenuButton_Click()
    Unload Me
        Menu.Show
    End Sub
    
    Private Sub UserForm_Initialize()
        Dim x As Long
        Dim k As Long
        Dim j As Long
    If MultiPage1.Value = 0 Then x = Sheets("INPUT BS").Range("D10:M50").Rows.Count
        ListBox1.List = Sheets("INPUT BS").Range("D10:M50").Value
        For k = 1 To x    'rows
            For j = 1 To 10    'columns
                ListBox1.List(k - 1, j - 1) = VBA.Format(ListBox1.List(k - 1, j - 1), "0")
            Next
        Next
    
        For j = 1 To 10
            Me.Controls("TextBox" & j).Text = ""
        Next j
        
    If MultiPage1.Value = 1 Then x = Sheets("INPUT BS").Range("D56:M200").Rows.Count
        ListBox2.List = Sheets("INPUT BS").Range("D56:M200").Value
        For k = 1 To x    'rows
            For j = 1 To 10    'columns
                ListBox2.List(k - 1, j - 1) = VBA.Format(ListBox2.List(k - 1, j - 1), "0")
            Next
        Next
    
        For j = 1 To 10
            Me.Controls("TextBox" & j).Text = ""
        Next j
        
    If MultiPage1.Value = 2 Then x = Sheets("INPUT BS").Range("D210:M257").Rows.Count
        ListBox3.List = Sheets("INPUT BS").Range("D210:M257").Value
        For k = 1 To x    'rows
            For j = 1 To 10    'columns
                ListBox3.List(k - 1, j - 1) = VBA.Format(ListBox3.List(k - 1, j - 1), "0")
            Next
        Next
    
        For j = 1 To 10
            Me.Controls("TextBox" & j).Text = ""
        Next j
    If MultiPage1.Value = 3 Then x = Sheets("INPUT BS").Range("D263:M282").Rows.Count
        ListBox4.List = Sheets("INPUT BS").Range("D263:M282").Value
        For k = 1 To x    'rows
            For j = 1 To 10    'columns
                ListBox4.List(k - 1, j - 1) = VBA.Format(ListBox4.List(k - 1, j - 1), "0")
            Next
        Next
    
        For j = 1 To 10
            Me.Controls("TextBox" & j).Text = ""
        Next j
    
    If MultiPage1.Value = 4 Then x = Sheets("INPUT BS").Range("D287:M502").Rows.Count
        ListBox5.List = Sheets("INPUT BS").Range("D287:M502").Value
        For k = 1 To x    'rows
            For j = 1 To 10    'columns
                ListBox5.List(k - 1, j - 1) = VBA.Format(ListBox5.List(k - 1, j - 1), "0")
            Next
        Next
    
        For j = 1 To 10
            Me.Controls("TextBox" & j).Text = ""
        Next j
        
    End Sub
    
    
    Private Sub CommandButton1_Click()
        Unload Me
    End Sub
    
    Private Sub CommandButton2_Click()
        Dim j As Long
        Dim x As Long
    
        If Me.ListBox1.ListIndex = -1 Then
            MsgBox "No Data Selected"
            Exit Sub
        End If
        
        If Me.TextBox1.Value = "" Then Exit Sub
    
        x = Me.ListBox1.ListIndex + 1    'row index
        With Sheets("INPUT BS")
            For j = 4 To 13    'columns Start in Column 3 <-----------
                .Cells(x, j).Value = Me.Controls("TextBox" & j - 2).Text '<--Start in TextBox1
            Next j
        End With
        Call UserForm_Initialize
    End Sub
    Private Sub CommandButton3_Click()
        Unload Me
    End Sub
    
    Private Sub CommandButton4_Click()
        Dim j As Long
        Dim x As Long
    
        If Me.ListBox2.ListIndex = -1 Then
            MsgBox "No Data Selected"
            Exit Sub
        End If
        
        If Me.TextBox13.Value = "" Then Exit Sub
    
        x = Me.ListBox2.ListIndex + 1    'row index
        With Sheets("INPUT BS")
               For j = 4 To 13  'columns Start in Column 3 <-----------
                .Cells(x, j).Value = Me.Controls("TextBox" & j + 10).Text '<--Start in TextBox13
            Next j
        End With
        Call UserForm_Initialize
    End Sub
    Private Sub CommandButton5_Click()
        Unload Me
    End Sub
    
    Private Sub CommandButton6_Click()
        Dim j As Long
        Dim x As Long
    
        If Me.ListBox3.ListIndex = -1 Then
            MsgBox "No Data Selected"
            Exit Sub
        End If
        
        If Me.TextBox25.Value = "" Then Exit Sub
    
        x = Me.ListBox3.ListIndex + 1    'row index
        With Sheets("INPUT BS")
            For j = 4 To 13  'columns Start in Column 3 <-----------
                .Cells(x, j).Value = Me.Controls("TextBox" & j + 22).Text '<--Start in TextBox25
            Next j
        End With
        Call UserForm_Initialize
    End Sub
    Private Sub CommandButton7_Click()
        Unload Me
    End Sub
    
    Private Sub CommandButton8_Click()
        Dim j As Long
        Dim x As Long
    
        If Me.ListBox4.ListIndex = -1 Then
            MsgBox "No Data Selected"
            Exit Sub
        End If
        
        If Me.TextBox37.Value = "" Then Exit Sub
    
        x = Me.ListBox4.ListIndex + 1    'row index
        With Sheets("INPUT BS")
            For j = 4 To 13  'columns Start in Column 3 <-----------
                .Cells(x, j).Value = Me.Controls("TextBox" & j + 34).Text '<--Start in TextBox37
            Next j
        End With
        Call UserForm_Initialize
    End Sub
    Private Sub CommandButton9_Click()
        Unload Me
    End Sub
    
    Private Sub CommandButton10_Click()
        Dim j As Long
        Dim x As Long
    
        If Me.ListBox5.ListIndex = -1 Then
            MsgBox "No Data Selected"
            Exit Sub
        End If
        
        If Me.TextBox49.Value = "" Then Exit Sub
    
        x = Me.ListBox5.ListIndex + 1    'row index
        With Sheets("INPUT BS")
            For j = 4 To 13  'columns Start in Column 3 <-----------
                .Cells(x, j).Value = Me.Controls("TextBox" & j + 46).Text '<--Start in TextBox49
            Next j
        End With
        Call UserForm_Initialize
    End Sub
    
    Private Sub ListBox1_Click()
        Dim k As Long
        Dim j As Long
        Dim Ctrl As Control
        For Each Ctrl In Me.Controls
            If TypeName(Ctrl) = "TextBox" Then
                k = Me.ListBox1.ListIndex    'row selected
                '            Debug.Print Me.ListBox1.ListIndex
                For j = 1 To 10
                    Me.Controls("TextBox" & j).Text = Me.ListBox1.List(k, j - 1)
                Next j
            End If
        Next Ctrl
    
        If Me.TextBox1.Value = "" Then
            MsgBox "Can't Select this Row"
            Exit Sub
        End If
    End Sub
    
    Private Sub ListBox2_Click()
        Dim k As Long
        Dim j As Long
        Dim Ctrl As Control
        For Each Ctrl In Me.Controls
            If TypeName(Ctrl) = "TextBox" Then
                k = Me.ListBox2.ListIndex    'row selected
                '            Debug.Print Me.ListBox2.ListIndex
                   For j = 1 To 10
                    Me.Controls("TextBox" & j + 12).Text = Me.ListBox2.List(k, j - 1) '<--Start in TextBox 13
                Next j
            End If
        Next Ctrl
    
        If Me.TextBox13.Value = "" Then
            MsgBox "Can't Select this Row"
            Exit Sub
        End If
    End Sub
    
    Private Sub Listbox3_Click()
        Dim k As Long
        Dim j As Long
        Dim Ctrl As Control
        For Each Ctrl In Me.Controls
            If TypeName(Ctrl) = "TextBox" Then
                k = Me.ListBox3.ListIndex    'row selected
                '            Debug.Print Me.Listbox3.ListIndex
                 For j = 1 To 10
                    Me.Controls("TextBox" & j + 24).Text = Me.ListBox2.List(k, j - 1) '<--Start in TextBox 25
                Next j
            End If
        Next Ctrl
    
        If Me.TextBox25.Value = "" Then
            MsgBox "Can't Select this Row"
            Exit Sub
        End If
    End Sub
    
    Private Sub Listbox4_Click()
        Dim k As Long
        Dim j As Long
        Dim Ctrl As Control
        For Each Ctrl In Me.Controls
            If TypeName(Ctrl) = "TextBox" Then
                k = Me.ListBox4.ListIndex    'row selected
                '            Debug.Print Me.Listbox4.ListIndex
                For j = 1 To 10
                     Me.Controls("TextBox" & j + 36).Text = Me.ListBox2.List(k, j - 1) '<--Start in TextBox 37
                Next j
            End If
        Next Ctrl
    
        If Me.TextBox37.Value = "" Then
            MsgBox "Can't Select this Row"
            Exit Sub
        End If
    End Sub
    
    Private Sub Listbox5_Click()
        Dim k As Long
        Dim j As Long
        Dim Ctrl As Control
        For Each Ctrl In Me.Controls
            If TypeName(Ctrl) = "TextBox" Then
                k = Me.ListBox5.ListIndex    'row selected
                '            Debug.Print Me.Listbox5.ListIndex
                For j = 1 To 10
                     Me.Controls("TextBox" & j + 48).Text = Me.ListBox2.List(k, j - 1) '<--Start in TextBox 48
                Next j
            End If
        Next Ctrl
    
        If Me.TextBox49.Value = "" Then
            MsgBox "Can't Select this Row"
            Exit Sub
        End If
    End Sub
    This should work using your example. But again getting error 381
    Last edited by Stingone; 12-04-2012 at 04:30 PM.

  10. #10
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Change listbox value

    Hi Stignone

    In the File I uploaded there is no UpDate Button...there is a Modify Button. So, are you saying "Nothing happens when you click the Modify Button" in the Sample File I posted?

  11. #11
    Registered User
    Join Date
    10-19-2012
    Location
    Assen
    MS-Off Ver
    Excel 2010
    Posts
    81

    Re: Change listbox value

    Hi John,

    No your file is working Ok. but see the last code i have provided. That is my complete model. But getting the error 381. I used your code as example

  12. #12
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Change listbox value

    Hi Stignone

    I can debug code on my file with my code but I've no way of knowing how to debug your code on your file without seeing your file and your code. Think about it.

    Be happy to help...if I can.

  13. #13
    Registered User
    Join Date
    10-19-2012
    Location
    Assen
    MS-Off Ver
    Excel 2010
    Posts
    81

    Re: Change listbox value

    Quote Originally Posted by jaslake View Post
    Hi Stignone

    I can debug code on my file with my code but I've no way of knowing how to debug your code on your file without seeing your file and your code. Think about it.

    Be happy to help...if I can.
    Attached the original file. In worksheet INPUT BS i have the DATA. Red marked i not want to change only display in listbox. Green marked needs to be changed.

    If you now run the file you will get the error 381.

    Really hope you can help me out.

    Original File.xlsm

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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