Delete all associated variables in a listbox items with same value

devofish

Board Regular
Joined
Dec 10, 2016
Messages
68
Hello all. I'm sure this is possible but I think I may be venturing into arrays, which I don't have a lot of understanding of. I have a listbox that populates a range of values (Strings) from a sheet. Each string value has a unique value attributed to it, however there are only a handful of string variables. The unique data value can be associated with different groups as well. For example, 2 columns (group, name) with n different groups with each group containing unique data values like below:
A B
Group1 value1
Group1 value2
Group2 value1
Group2 value2
Group3 value3
Group4 value1
Group4 value2
Group5 value1
...
The listbox shows up:
Group1
Group2
Group3
Group4
...
Right now my delete routine can select say Group2 (which then removes value1) and removes the row and moves up, however value2 remains obviously. I would like to remove all values (rows and objectively Group2 as well) associated with Group2 and move rows up as the current routine does. Since The Groups values can grow to n+1, how would vba identify that all values associated with a listbox selected group be removed? Below is my delete code to remove only one line at a time:
Code:
Private Sub cmdDelete_Click()

    ' Deletes items and values based on user defined selection through controls
    ' if deletion is made within middle of range, data is moved up
    Rw = Me.ListBox1.ListIndex + 2
    If Me.ListBox1.ListIndex = -1 Then
    MsgBox "You haven't selected a group to delete."
        ElseIf MsgBox("You are about to delete " & TextBox1.Text & _
        " from the Group Distributions and ALL assoicated data with this group. Do you wish to continue?", _
        vbYesNo + vbDefaultButton2, "Delete Group") = vbNo Then
        Exit Sub
    End If
    If Me.ListBox1.ListIndex > -1 Then Sheets(msSHEET_NAME).Cells(Rw, 1).EntireRow.Delete
        With ThisWorkbook.Sheets(msSHEET_NAME)
            TextBox1.Text = ""
    End With
    FillListBox
End Sub
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.

Forum statistics

Threads
1,223,270
Messages
6,171,102
Members
452,379
Latest member
IainTru

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