Deleting data selected items listbox

Bandito1

Board Regular
Joined
Oct 18, 2018
Messages
233
Office Version
  1. 2016
Platform
  1. Windows
Hi everyone,

I got this listbox named lstSearchResults.
The data in it comes through VBA from the worksheet "Telephone".
I would like to have a button to delete items in the listbox that i select.

I found this code;
Code:
Private Sub cmdDeleteContact_Click()
Dim i As Long


With Me.lstSearchResults
For i = .ListCount - 1 To 0 Step -1
If .Selected(i) = True Then
.RemoveItem i
End If
Next i
End With
End Sub

The item in the listbox gets deleted but i would like that the data also gets deleted in the worksheet.
How can i add this?
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
You need find row of sheet (j) how with (i) on lstSearchResults. Then you convert (i) for (j), so you can use (j) to Delete cells on Sheet.

Code:
'Delete 1 row
Rows(j).Delete

'Or clear cell
Range("B" & j).ClearContents
 
Upvote 0
Hi Bandito1,

This should do the job:

Code:
Option Explicit
Private Sub cmdDeleteContact_Click()

    Dim i As Long
    Dim lngMyRow As Long
    
    Application.ScreenUpdating = False

    With Me.lstSearchResults
        For i = .ListCount - 1 To 0 Step -1
            If .Selected(i) = True Then
                If IsNumeric(.List(i)) = True Then
                    lngMyRow = Evaluate("IFERROR(MATCH(" & .List(i) & ",Telephone!A:A,0),0)")
                Else
                    lngMyRow = Evaluate("IFERROR(MATCH(""" & .List(i) & """,Telephone!A:A,0),0)")
                End If
                If lngMyRow > 0 Then
                    Sheets("Telephone").Rows(lngMyRow).EntireRow.Delete
                    .RemoveItem i
                End If
            End If
        Next i
    End With
    
    Application.ScreenUpdating = True

End Sub

Regards,

Robert
 
Upvote 0
I should have mentioned that the code looks for the phone numbers in column A (change to suit) and will only delete one matching entry (they should all be unique so that shouldn't be an issue).
 
Upvote 0
Hello Robert and and Sadboy,

Thanks for your replies and time.
The code seems like to work, thank you very much.

It happens that i have names in my list without phone number. Rest of the numbers are unique.
I was afraid the code would crash if i try to delete someone without phonenumber but that doesn't happen.
 
Upvote 0
Hi guys,

I tested the code out but when i delete the last row in the listbox the whole list gets deleted.
So when there are 5 items in it i can safely delete 1 - 4 but when i delete #5 everything gets deleted at once..
You know where the mistake is being made?

Code:
Private Sub cmdDeleteContact_Click()

    Dim i As Long
    Dim lngMyRow As Long
    Dim varResponse As Variant
    
varResponse = MsgBox("Are you sure you want to delete the selected person?", vbYesNo, "Confirmation")


If varResponse <> vbYes Then Exit Sub


    Application.ScreenUpdating = False


    With Me.lstSearchResults
        For i = .ListCount - 1 To 0 Step -1
            If .Selected(i) = True Then
                If IsNumeric(.List(i)) = True Then
                    lngMyRow = Evaluate("IFERROR(MATCH(" & .List(i) & ",Telephone!A:A,0),0)")
                Else
                    lngMyRow = Evaluate("IFERROR(MATCH(""" & .List(i) & """,Telephone!A:A,0),0)")
                End If
                If lngMyRow > 0 Then
                    Sheets("Telephone").Rows(lngMyRow).EntireRow.Delete
                    .RemoveItem i
                End If
            End If
        Next i
    End With
    
    Application.ScreenUpdating = True


End Sub
 
Upvote 0

Forum statistics

Threads
1,215,347
Messages
6,124,421
Members
449,157
Latest member
mytux

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