Update range for each selected item in Multi Select Listbox

sharky12345

Well-known Member
Joined
Aug 5, 2010
Messages
3,422
Office Version
  1. 2016
Platform
  1. Windows
I need to update a column for each item selected in a Multi Select Listbox on a Userform.

I'm using this to load the Listbox;

Code:
With Sheet5
Dim Cl As Range
Dim Lst As String
For Each Cl In Range("A2:A42")
 If Cl <> "" And Cl.Offset(, 2).Value > 1 Then
 If Lst = "" Then
Lst = Cl.Value
Else
 Lst = Lst & "," & Cl.Value
 End If
 End If
 Next Cl
 ListBox1.List = Split(Lst, ",")
 End With

I need to update column F for each selected value with "YES" if someone can show me how please?
 
Last edited:
I appreciate your patience. It hasn't been long since I learned to code on my own, and I'm afraid that I may be jumping too far and too fast for me to actually understand what the codes do.

So the code I use to populate the userform with dates that correspond to the user is also the userform initialize script:
Code:
Private Sub UserForm_Initialize()


Dim rng As Range
Dim Lastrow As Long
Dim i As Long


Lastrow = Range("b" & Rows.Count).End(xlUp).Row
    
For i = 1 To Lastrow
    Set rng = Range("a" & i)
    If rng.Offset(0, 2) = Environ("username") Then
        ListBox1.AddItem rng.Value
    End If
Next i


End Sub

And now my goal is to write a code into a button in that userform that writes cancel in col D for each of the dates selected in the listbox. I know that I'm supposed to be writing a loop, but I'm not sure how exactly I should be looping through entries in a listbox or if I may be overly complicating my situation.
 
Upvote 0

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Try this:
Replace Rob with
Environ("username")


Code:
Private Sub CommandButton6_Click()
'Modified  8/13/2018  11:01:21 PM  EDT
Dim Lastrow As Long
Lastrow = Cells(Rows.Count, "b").End(xlUp).Row
Dim i As Long
Dim b As Long
        For i = 0 To ListBox1.ListCount - 1
            If ListBox1.Selected(i) = True Then
            
                For b = 1 To Lastrow
                    If Cells(b, 3).Value = "Rob" And Cells(b, 1) = DateValue(ListBox1.List(i)) Then Cells(b, 4).Value = "Cancelled By Assoc"
                Next
            End If
        
        Next
        
End Sub
 
Upvote 0
Amazing, it works just the way I needed it to. I knew I had to loop each selected value in the listbox through the dates in col A, so I tried to come up with this code on my own yesterday but I could not find out why it wouldn't work even if my life depended on it.

Code:
Dim i As Long
Dim rng As Long
rng = Range("a" & Rows.Count).End(xlUp).Row
For i = 0 To ListBox1.ListCount - 1
    With ListBox1
    If .Selected(i) = True Then
        For Each cell In Range("a1:a" & rng).Cells
            If cell.Value = .list(i) Then
                If cell.Offset(0, 2).Value = Environ("username") Then
                    cell.Offset(0, 3).Value = "Cancelled By Assoc"
                End If
            End If
        Next cell
    End If
    End With
Next i

Thank you for your help and may the deities of Excel keep you throughout your days.
 
Upvote 0
Glad I was able to help you.
Come back here to Mr. Excel next time you need additional assistance.

I see your not using the script I provided. But it's good to see you know how to write scripts the way you want. There are always 10 ways to do everything in Excel. But I guess my script helped you achieve your goal.
 
Last edited:
Upvote 0
Sorry, it looks like my last response made the wrong impression. I am using the exact script you made and I sincerely appreciate it. I just posted my non-functioning code as comparison to yours so I (and maybe others) could see where I failed.
 
Upvote 0
That's great. I'm really glad I have things working for you. It took me a little while to figure it out myself. I had not worked with Multiselect listbox's in a while so I had to refresh my memory. So working on this project helped us both. Take care.
 
Upvote 0

Forum statistics

Threads
1,224,829
Messages
6,181,218
Members
453,024
Latest member
Wingit77

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top