You are here:

- Home
- Computing/Technology
- Business Software
- Excel
- Delete rows from sheet using values listed on another sheet in the same workbook

Advertisement

Using Excel 2003

Hi Tom,

I have been using this macro I found on an excel website to delete rows from a sheet named Issues where the values in column I (starting with Row 2) are equal to any value found in column A of a sheet named MyList. Now I’m trying to do the opposite thing and delete the rows where the values are not equal.

I thought I could just change this line .Range("I1:I" & .Rows.Count).AutoFilter field:=1, Criteria1:=cell.Value

to .Range("I1:I" & .Rows.Count).AutoFilter field:=1, Criteria1:="<>" & cell.Value

but this just deletes all the rows in the Issues sheet. Is it possible to modify this code to delete the rows when the values are not equal?

Thanks, Anthony

Sub Handle_List()

Dim cell As Range

Dim Rng As Range

Dim CriteriaRng As Range

Dim calcmode As Long

With Application

calcmode = .Calculation

.Calculation = xlCalculationManual

.ScreenUpdating = False

End With

With Sheets("MyList")

Set CriteriaRng = .Range("A2", .Cells(Rows.Count, "A").End(xlUp))

End With

'Loop through the cells in the Criteria range

For Each cell In CriteriaRng

With Sheets("Issues")

.AutoFilterMode = False

'Apply the filter (to column I)

.Range("I1:I" & .Rows.Count).AutoFilter field:=1, Criteria1:=cell.Value

With .AutoFilter.Range

Set Rng = Nothing

On Error Resume Next

Set Rng = .Offset(1, 0).Resize(.Rows.Count - 1, 1) _

.SpecialCells(xlCellTypeVisible)

On Error GoTo 0

If Not Rng Is Nothing Then Rng.EntireRow.Delete

End With

'Remove the AutoFilter

.AutoFilterMode = False

End With

Next cell

With Application

.ScreenUpdating = True

.Calculation = calcmode

End With

End Sub

Anthony,

for example, in A let's say you have the letters

A

B

in your data range in column I, let's say you have the letters

A

B

C

D

when you filter on <>A, then B, C and D are deleted

then when you filter on <>B, A is delete

so that is why you can't use the approach you describe.

You would pretty much need to do something like this to delete rows that contain a value in column I that is not found any cells in the criteria list (what I think you want)

Sub Handle_List_Opposite()

Dim cell As Range

Dim Rng As Range

Dim Data as Range

Dim CriteriaRng As Range

Dim calcmode As Long

With Application

calcmode = .Calculation

.Calculation = xlCalculationManual

.ScreenUpdating = False

End With

With Sheets("MyList")

Set CriteriaRng = .Range("A2", .Cells(Rows.Count, "A").End(xlUp))

End With

With Sheets("Issues")

set Data = .Range("I1",.Cells(.rows.count,"I").End(xlup))

end with

for each cell in Data

if application.Countif(CriteriaRng, cell) = 0 then

cell.clearcontents

end if

next

on error resume Next

Data.SpecialCells(xlblanks).EntireRow.Delete

on Error goto 0

With Application

.ScreenUpdating = True

.Calculation = calcmode

End With

End Sub

--

Regards,

Tom Ogilvy

- Add to this Answer
- Ask a Question

Rating(1-10) | Knowledgeability = 10 | Clarity of Response = 10 | Politeness = 10 |

Comment | Thanks Tom. This is exactly what I need to do and double thanks for the explanation. I’m embarrassed that I did not see why my approach would not work. Anthony |

This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here

Excel

Answers by Expert:

Selected as an Excel MVP by Microsoft since 1999. Answering Excel questions in Allexperts since its inception in 2001. Able to answer questions on almost all aspects of Excel's internal capabilities. If seeking a VBA solution, please specify that in your question itself so I give you the answer you want. [Excel has weak protection - if you are distributing an application, I don't answer questions on how to protect your project from your users.]

Extensive experience. **Education/Credentials**

Master of Science (MS) degree Operations Research (ORSA)**Awards and Honors**

Microsoft MVP in Excel.