Consulting

Results 1 to 14 of 14

Thread: Update column of selected items in Multi Select Listbox

  1. #1
    Banned VBAX Regular
    Joined
    Aug 2010
    Posts
    54
    Location

    Update column of selected items in Multi Select Listbox

    I have a Listbox on a Userform which gets it's values from this;

    Dim lastrow As Long
    With Sheet1
    lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
    End With
    With Me.ListBox1
    .ColumnCount = 15
    .ColumnHeads = True
    .ColumnWidths = "50;60;50;65;0;0;0;0;0;0;0;0;0;0;0"
    .RowSource = Sheet1.Range("A2:O" & lastrow).Address(, , , True)
    End With
    I need to update column D of each row if the item is selected in the Listbox and have made a little progress, I've got this working, sort of, but it only updates the row for the first selected item and doesn't loop through them all of someone can help? The code I have so far is here;

    Dim rngSearch As Range
    Dim rngFound As Range
    Dim i As Long
    CheckedCalendarFrm.Show
    Set rngSearch = Sheet1.Range("B2:B25")
    With Me.ListBox1
    For i = 0 To .ListCount - 1
    If .Selected(i) Then
    Set rngFound = rngSearch.Find(what:=.List(i, 1), LookIn:=xlFormulas, lookat:=xlWhole, MatchCase:=False)
    If Not rngFound Is Nothing Then
    rngFound(1, 3).Value = TextCheckDate
    End If
    End If
    Next i
    End With
    Also posted here: https://www.mrexcel.com/forum/excel-...t-listbox.html

  2. #2
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    check if the second column value .List(i, 2) of selected item(s) exists in B2:B25

    you may upload your workbook.
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  3. #3
    Banned VBAX Regular
    Joined
    Aug 2010
    Posts
    54
    Location
    Thanks - I don't understand why you're asking me to check that .List(i, 2) exists because that doesn't feature anywhere in the code?

    I can't upload a workbook at this stage, I'll have to sanitise it first.

  4. #4
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    Private sub Listbox1_change()
       if listbox1.selected(listbox1.listindex) then listbox1.column(3)=date
    End sub

  5. #5
    Banned VBAX Regular
    Joined
    Aug 2010
    Posts
    54
    Location
    Thanks, but I get 'Method or data member not found' on this part;

    .Columns(3) =

  6. #6
    Banned VBAX Regular
    Joined
    Aug 2010
    Posts
    54
    Location
    *bump*

  7. #7
    Banned VBAX Regular
    Joined
    Aug 2010
    Posts
    54
    Location
    Wasn't necessary to be rude - I came here for help, not to be chastised like a 4 year old!

    In any event, check your suggestion before you reply - it doesn't work and produces an error!

  8. #8
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    it doesn't

  9. #9
    Banned VBAX Regular
    Joined
    Aug 2010
    Posts
    54
    Location
    It does;

    Untitled.jpg

    but disregard.

  10. #10
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    Don't ever use rowsource to populate a combobox/listbox.

  11. #11
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,875
    When part of the .rowsource of a listbox changes it appears to deselect the selection!
    So grab a list of selected rows first, then process them.
    Just tweaking your code a little:
    Dim SelectedRows()
    ReDim SelectedRows(1 To 1)
    idx = 0
    With Me.ListBox1
      For i = 0 To .ListCount - 1
        If .Selected(i) Then
          idx = idx + 1
          ReDim Preserve SelectedRows(1 To idx)
          SelectedRows(idx) = i
        End If
      Next i
      Set rngSearch = Sheet1.Range("B2:B25")
      TextCheckDate = "zzz"  'readjust this
      For Each SelectedRow In SelectedRows
        Set rngFound = rngSearch.Find(what:=.List(SelectedRow, 1), LookIn:=xlFormulas, lookat:=xlWhole, MatchCase:=False)
        If Not rngFound Is Nothing Then
          rngFound(1, 3).Value = TextCheckDate
        End If
      Next SelectedRow
    End With
    but this is not robust, because if you have two values in column B the same, the .Find process will only ever find the first.
    The following will circumvent that:
    Dim SelectedRows()
    idx = 0
    ReDim SelectedRows(1 To 1)
    With Me.ListBox1
      For i = 0 To .ListCount - 1
        If .Selected(i) Then
          idx = idx + 1
          ReDim Preserve SelectedRows(1 To idx)
          SelectedRows(idx) = i
        End If
      Next i
      Set yyy = Range(.RowSource)
      TextCheckDate = "zzz"
      For Each SelectedRow In SelectedRows
       yyy.Cells(SelectedRow + 1, 4).Value = TextCheckDate
      Next SelectedRow
    '  'reinstate selected items:
    '  For Each SelectedRow In SelectedRows
    '    ListBox1.Selected(SelectedRow) = True
    '  Next SelectedRow
    End With
    Last edited by p45cal; 02-25-2017 at 12:40 PM.
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  12. #12
    Banned VBAX Regular
    Joined
    Aug 2010
    Posts
    54
    Location
    Thank you - does exactly what I need.

  13. #13
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,875
    Note that I added some commented-out code to reinstate the selected items at the end, if you want.
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  14. #14
    Banned VBAX Regular
    Joined
    Aug 2010
    Posts
    54
    Location
    Got that - many thanks.

Posting Permissions

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