UserForm VBA that Deletes Multiple Rows from worksheet

powerpivotlegal

New Member
Joined
May 14, 2014
Messages
30
Hello,

I've been slowly building out multiple user forms through the help of the community. I need a Delete Command Button in my Search User Form that loops through my worksheet and deletes multiple rows that equal the Project Name selected in the Search User Form.

For example, user selects "Project XYZ" from the combo box Search User Form. Project XYZ is listed twice in the worksheet under Column A because there are multiple team members in Column C (row 100 is for Team Member Joe and row 101 is Team Member Jane).
When the user then clicks on the Delete Command Button to delete Project XYZ, the code that I found below only deletes row 100 and not both rows 100 and 101.

How can the code be fixed to search through a dynamic range where Column A is expanding and delete all rows that equal the value in Column A?


Private Sub DeleteCommandButton_Click()
Dim cName As String
Dim i As Long


cName = ProjectNameSearchComboBox.Value


For i = 1 To 200


If Range("A" & i).Value = cName Then
ans = MsgBox("Delete " & cName & " from list?", vbYesNo)
If ans = vbYes Then
Range("A" & i).EntireRow.Delete
End If
End If
Next i
End Sub

Many thanks in advance!

- James
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Filter column A by the item you want to delete and if any rows match, delete them.

Code:
Sub DeleteMatchesFromColumnA(sProjectToDelete As String)
    
    Dim lDelCount As Long

    With ActiveSheet
        .AutoFilterMode = False
        .Range("A1").CurrentRegion.AutoFilter Field:=1, Criteria1:=sProjectToDelete, Operator:=xlAnd
        lDelCount = Application.WorksheetFunction.Subtotal(3, .Columns(1)) - 1  '-1 to not count header
        If lDelCount > 0 Then
            'row other than header is visible
            .Range("A1").CurrentRegion.Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete
            'Offset in above line to preserve row 1, headings
        End If
        .AutoFilterMode = False
        Debug.Print lDelCount & " instances of " & sProjectToDelete & " in column A were deleted."
    End With
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,566
Messages
6,125,597
Members
449,238
Latest member
wcbyers

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