Hi . I am looking to find all cells that have 5 digits in (they are addresses so will have other text also in cell) and then delete all rows at once

nickkmal

New Member
Joined
Mar 21, 2022
Messages
8
Office Version
  1. 365
Platform
  1. Windows
For example, I want to find (in this example) 90017 and then delete this whole row, and I have hundreds of similar cells on the spreadsheet where I would all cells highlighted that have 5 digits and the whole row of those highlighted cells deleted

3107 Doctors Drive, Los Angeles, California-90017 Doctors Drive Doctors Drive 90017
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Welcome to the Board!

A few questions about your worksheet:
1. How many columns of data are there?
2. Is there a header/title row? If so, what row(s) are they?
3. For all your rows with data, is there always data in column A?
 
Upvote 0
Let's say that your data is in columns A and B, your header row is row 1, and the column you are looking for these values in column A.

Then you can use this VBA code, which will prompt the user for the value to look for, to delete those rows:
VBA Code:
Sub MyDeleteMacro()

    Dim str As String
    Dim lr1 As Long
    Dim lr2 As Long
    Dim rng As Range
    
'   Prompt user what value to look for and delete
    str = InputBox("What string would you like to look for and delete those rows?")
    
'   Find last row with data in column A
    lr1 = Cells(Rows.Count, "A").End(xlUp).Row

'   Create range to filter
    Set rng = Range("A1:B" & lr1)

'   Filter range
    'rng.AutoFilter
    rng.AutoFilter Field:=1, Criteria1:="=*" & str & "*" _
        , Operator:=xlAnd
        
'   Find last row in column A with data after filter
    lr2 = Cells(Rows.Count, "A").End(xlUp).Row

'   Exit sub if no data to delete data (only header visible)
    If lr2 = 1 Then Exit Sub

'   Delete unhidden data
    Application.DisplayAlerts = False
    ActiveSheet.UsedRange.Offset(1, 0).Resize(ActiveSheet.UsedRange.Rows.Count - 1).Rows.Delete
    Application.DisplayAlerts = True
    
'   Remove filter
    rng.AutoFilter
    
End Sub
 
Upvote 0
HI, firstly thanks so much for taking the time to reply. Sadly I am only sufficiently proficient to use in-cell calculations / formulas, I dont know anything about VBA code or how to apply it. Is there an in-cell formula you know of?

Thanks again
 
Upvote 0
Excel formulas can only return values to the cells that they are in. They cannot perform action, like the deleting of rows.
So you cannot physically delete rows without VBA, unless you want to do it manually.

The closest thing I think you can use is Filters. With basic Filters, you can hide the rows you do not want (but not delete them).
With Advanced Filters, you can filter the results to a new location (i.e. a new sheet), so you can filter just the rows you want to keep to the new location.

Note that the VBA code should do all you want, and you actually do not need to fully understand to be able to use it.
Here is a short tutorial that shows you exactly where to insert (copy/paste) the code I wrote to place it in your worksheet, and also shows you how to run it:
 
Upvote 0

Forum statistics

Threads
1,215,761
Messages
6,126,735
Members
449,334
Latest member
moses007

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