Deleting listbox list, from listbox and source

blaker

New Member
Joined
Jul 1, 2013
Messages
32
Office Version
  1. 365
Platform
  1. Windows
I would like to double click on one of the items / rows of data in the list box and delete that item in the list box as well as the source in the spreadsheet. this is multi column, the sheet is Market and the Range is "AA7:aq26" when the listbox data is loaded. I have the below code but it only deletes the row from the list box not the source?

Private Sub ListBox3_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
Dim x As Long
Dim OriginalCount As Long

'Store original ListBox count
OriginalCount = ListBox3.ListCount



'Delete selected line items
For x = OriginalCount - 1 To 0 Step -1
If ListBox3.Selected(x) = True Then ListBox3.RemoveItem x
Next x

see below the form open in front of the spreadsheet sorcedata for the listbox.
Thanks!
Blake

1622669498281.png
 
Okay, let's try to delete the record using column AK as a key.

Update your ListBox3_DblClick code with the following:

VBA Code:
Private Sub ListBox3_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
  Dim x As Long
  Dim OriginalCount As Long
  Dim f As Range
  Dim sh As Worksheet
  Dim customer As String
  
  Set sh = Sheets("Market")
  'Delete selected line items
  For x = ListBox3.ListCount - 1 To 0 Step -1
    If ListBox3.Selected(x) = True Then
      customer = ListBox3.List(x, 10)
      If customer <> "" Then
        Set f = sh.Range("AK:AK").Find(customer, , xlValues, xlWhole, , , False)
        If Not f Is Nothing Then
          f.EntireRow.Delete
          ListBox3.RemoveItem x
        End If
      End If
    End If
  Next x
End Sub
 
Upvote 0

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Hey this works perfectly! however I have formulas in the adjacent cells that calculate cost and don't really want to delete the entire row, would there be a way to in the selected row delete the values in "Y:AD", "AF:AM" and "AO" and the shift cells up...
is there a way to do this? never the less I really appreciate the code you have provided so far!!!
Thanks!
Blake
 
Upvote 0
delete the values in "Y:AD", "AF:AM" and "AO" and the shift cells up
Try this:

VBA Code:
Private Sub ListBox3_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
  Dim x As Long
  Dim OriginalCount As Long
  Dim f As Range
  Dim sh As Worksheet
  Dim customer As String
  
  Set sh = Sheets("Market")
  'Delete selected line items
  For x = ListBox3.ListCount - 1 To 0 Step -1
    If ListBox3.Selected(x) = True Then
      customer = ListBox3.List(x, 10)
      If customer <> "" Then
        Set f = sh.Range("AK:AK").Find(customer, , xlValues, xlWhole, , , False)
        If Not f Is Nothing Then
          sh.Range("Y" & f.Row & ":AD" & f.Row & _
                   ", AF" & f.Row & ":AM" & f.Row & _
                   ", AO" & f.Row).Delete shift:=xlUp
          ListBox3.RemoveItem x
        End If
      End If
    End If
  Next x
End Sub
 
Upvote 0
Solution
Dim x As Long Dim OriginalCount As Long Dim f As Range Dim sh As Worksheet Dim customer As String Set sh = Sheets("Market") 'Delete selected line items For x = ListBox3.ListCount - 1 To 0 Step -1 If ListBox3.Selected(x) = True Then customer = ListBox3.List(x, 10) If customer <> "" Then Set f = sh.Range("AK:AK").Find(customer, , xlValues, xlWhole, , , False) If Not f Is Nothing Then sh.Range("Y" & f.Row & ":AD" & f.Row & _ ", AF" & f.Row & ":AM" & f.Row & _ ", AO" & f.Row).Delete shift:=xlUp ListBox3.RemoveItem x End If End If End If Next x
This works perfect!!!

Thanks so much!!
 
Upvote 0
I'm glad to help you. Thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,985
Messages
6,122,606
Members
449,089
Latest member
Motoracer88

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