How to Delete Multiple cells using ListBox Multi selection

sai85

New Member
Joined
Apr 29, 2013
Messages
49
If I have selected more than one selection in List Box, I am not able to delete all of my selected values. It either Deleting from Top or Bottom. I have already searched for codes in internet and whatever i got, it is not working for me.Code given for your reference:

Private Sub Cmd_Del_Click()
Dim sh As Worksheet
Set sh = ThisWorkbook.Worksheets("Sheet1")
Dim i As Long
For i = Me.LB_ZoneRegion.ListCount - 1 To 0 Step -1
If Me.LB_ZoneRegion.Selected(i) = True Then
sh.Range("A" & i + 2 & ":B" & i + 2).Select
Selection.Delete
End If
Next i
Call UserForm_Initialize
End Sub


Private Sub UserForm_Initialize()
On Error Resume Next
With Me.LB_ZoneRegion
.Clear
.ColumnCount = 2
.ColumnHeads = True
.ColumnWidths = "40;50"
.RowSource = "ZoneRegion"
.MultiSelect = fmMultiSelectMulti
End With
End Sub
 
Last edited:

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
In what way doesn't that code work?
 
Upvote 0
Try this:-
Code:
Private [COLOR="Navy"]Sub[/COLOR] Cmd_Del_Click()
[COLOR="Navy"]Dim[/COLOR] sh [COLOR="Navy"]As[/COLOR] Worksheet, Rng [COLOR="Navy"]As[/COLOR] Range
 [COLOR="Navy"]Set[/COLOR] sh = ThisWorkbook.Worksheets("Sheet1")
 [COLOR="Navy"]Dim[/COLOR] i [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
 [COLOR="Navy"]For[/COLOR] i = 0 To Me.LB_ZoneRegion.ListCount - 1
    [COLOR="Navy"]If[/COLOR] Me.LB_ZoneRegion.Selected(i) [COLOR="Navy"]Then[/COLOR]
        [COLOR="Navy"]If[/COLOR] Rng [COLOR="Navy"]Is[/COLOR] Nothing [COLOR="Navy"]Then[/COLOR]
            [COLOR="Navy"]Set[/COLOR] Rng = Range("A" & i + 1).Resize(, 2)
        [COLOR="Navy"]Else[/COLOR]
            [COLOR="Navy"]Set[/COLOR] Rng = Union(Rng, Range("A" & i + 1).Resize(, 2))
        [COLOR="Navy"]End[/COLOR] If
    [COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]Next[/COLOR] i
[COLOR="Navy"]If[/COLOR] Not Rng [COLOR="Navy"]Is[/COLOR] Nothing [COLOR="Navy"]Then[/COLOR] Rng.EntireRow.Delete
Call UserForm_Initialize
 [COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0

Forum statistics

Threads
1,214,918
Messages
6,122,257
Members
449,075
Latest member
staticfluids

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