Delete ALL rows that match textbox.value on UserForm

03856me

Active Member
Joined
Apr 4, 2008
Messages
297
I have a simple userform that contains one textbox named txt_unitnumber. I am trying to delete ALL rows (there could be 5-50 rows) on worksheet "data" that contain the unit number in column N. I have put together this code and I do not get any errors when I execute the delete button, but it does not delete the expected rows.

1. how do I get this code to work? The unit number in column N is formatted a "general" if that matters!
2. my data table is large, and would like the code to look from the bottom up, instead of the top down, how would I do that. - I understood from research that would speed up the macro.

Your help is greatly appreciated.

Code:
Private Sub cmd_delete_Click()
    Dim ws As Worksheet
        Set ws = Worksheets("data")
         
         ' Edit database
        Set rngfind = ws.Range("N:N").Find(what:=Me.txt_unitnumber.Value, MatchCase:=True)
        If Not rngfind Is Nothing Then
            rngfind.EntireRow.Delete
        End If
    ClearUnitNumber
End Sub
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
A little different approach would be to use a filter.
Something along the lines of this (untested)
Code:
Private Sub cmd_delete_Click()
' Remove Rows based on one column only
    Dim ws As Worksheet
    Dim lr As Long
    
Application.ScreenUpdating = False

Set ws = Sheets("data")      '<--alter as needed

'find very last used cell
With ws
    lr = .Cells.Find("*", .Cells(.Rows.Count, .Columns.Count), SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    'filter column
    .Range("N1:N" & lr).AutoFilter field:=1, Criteria1:=Me.txt_unitnumber.Value   
    'delete all visible rows except header
    On Error Resume Next    'in case there are none to delete
    .Range("N2:N" & lr).SpecialCells(xlCellTypeVisible).EntireRow.Delete    '<--alter start row to exclude header(s)
    On Error GoTo 0         'turn error checking back on
    'remove filter
    .AutoFilterMode = False
End With

Application.ScreenUpdating = True

End Sub
 
Upvote 0
This worked pretty well, EXCEPT it did not remove the filter from the worksheet, idea? Also, it worked really fast, which is wonderful.
 
Upvote 0

Forum statistics

Threads
1,213,560
Messages
6,114,304
Members
448,564
Latest member
ED38

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