Log in

View Full Version : How to delete cells' values through a multiselect listbox?



MrBlackd
08-05-2014, 06:38 PM
Hi to all,

I have the following issue. Say for example that I have in Sheet1 the following A1: title and A2:A11 a named range called test_range

I have made Userform1 that contains Listbox1 and command button Delete
I have used .RowSource to populate the items of the listbox and I have also set the listbox to be multiselect.

I want to select one or more items in the listbox and upon pressing delete I want the respective cells in the named range to have a zero value.

I have the following code but it deletes only the first of the selected items and not all of them.

Any help would be handy... thanks in advance



Private Sub Delete_Click()
Dim strRange As String
With ListBox1
strRange = .RowSource
For I = 0 To .ListCount - 1
If .Selected(I) = True Then
Range(strRange).Cells(I + 1, 1).Value = 0
End If
Next
.RowSource = vbNullString
.RowSource = strRange
End With
End Sub

snb
08-05-2014, 07:09 PM
Why don't you change the values in the listbox of the items you want to be deleted to 0 ?

MrBlackd
08-06-2014, 12:10 AM
Why don't you change the values in the listbox of the items you want to be deleted to 0 ?
What exactly do you suggest? Could you give me an example?

snb
08-06-2014, 12:43 AM
See the attachment.
Show the userform by clicking the button 'start'
To set an item in the listbox to '0': doubleclick it.