ListBox - Remove Item from source

Big Lar

Well-known Member
Joined
May 19, 2002
Messages
557
Can I get suggestions for the additional code needed to remove the selected item from Sheet6?
This only removes the item from the UserForm.
Rich (BB code):
Rich (BB code):
Private Sub UserForm_Initialize() With Sheet6 ListBox1.List = .Range("A2", .Range("A" & .Rows.Count).End(xlUp)).Resize(, 2).Value End With End Sub _________________________________________________________________________________ Private Sub CommandButton3_Click() Dim i As Integer
For i = 0 To ListBox1.ListCount - 1 If ListBox1.Selected(i) Then ListBox1.RemoveItem (i) End If Next i End Sub
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
The line with the RemoveItem command is where you've identified what is being removed. There, you'd need to add a line to remove the desired field from your sheet.

Code:
Private Sub CommandButton3_Click()
Dim i As Integer
With Sheet6
    For i = ListBox1.ListCount - 1 To 0 Step -1
        If ListBox1.Selected(i) Then
           ListBox1.RemoveItem (i)
           .Range(.Cells(i + 2, 1), .Cells(i + 2, 2)).Delete Shift:=xlUp
        End If
    Next i
End With
End Sub
 
Upvote 0
Thanks for the assistance.

I’ve replaced my code with your suggestion, and it works nicely…however, there’s one issue:
When selecting the last item in the ListBox, CommandButton3 deletes all items.

Debugging shows each Item being selected and deleted as it loops through the Next i. Again, this only occurs when the last ListBox item is selected.
 
Upvote 0
Do you have the ListBox1.MultiSelect set to multi-select or single-select? If you are single selecting, you could put an Exit Sub line right after the delete line. I tested on a sample set with multi-select and selected the last item and it only deleted the last entry.
 
Upvote 0
Thanks CalcSux78.
Changing the property to Multi-Select is the solution.
Thanks again for your assistance.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,324
Members
452,635
Latest member
laura12345

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