delete rows

Public Schedule Face-to-Face & Online Instructor-Led Training - View dates & book

Forum home » Delegate support and help forum » Microsoft Excel VBA Training and help » Delete rows

Delete rows

resolvedResolved · High Priority · Version 2010

Ross has attended:
Excel VBA Intro Intermediate course

Delete rows

Hi im trying to create a macro that deletes and entire row if the cells in the range does does not equal a certain criteria

Range C7:C822
Criteria is other


the macro i have written below does not seem to delete all the other rows??


Sub delete()

For Each C In range("C7:C822")
If C <> "Other" Then C.EntireRow.delete
Next

End Sub

regards
Ross

RE: Delete rows

Hi Ross

I've tried your example and it only deletes some of the cells that are not other. If you try changing EntireRow.delete with ClearContents then it does work but leaves gaps for the 'non other' values. The problem is that when an entire row is deleted then the next value of c is missed out. Here's an alternative suggestion for the code.

Range("C7").Select
Do
If ActiveCell.Value <> "other" Then
ActiveCell.EntireRow.Delete
Else
ActiveCell.Offset(1, 0).Select
End If
Loop Until ActiveCell.Value = ""

Note also that "other" is different from "Other" unless you add the line Option Compare Text in the declaration section.

If I find a way using your range variable c I'll let you know.

Cheers
Doug Dunn
Best STL

 

Training courses

 

Training information:

Welcome. Please choose your application (eg. Excel) and then post your question.

Our Microsoft Qualified trainers will then respond within 24 hours (working days).

Frequently Asked Questions
What does 'Resolved' mean?

Any suggestions, questions or comments? Please post in the Improve the forum thread.


 

Excel tip:

Changing the Tab Colour of an Excel 2010 Worksheet

Did you know you could give the tabs in your worksheet different colours?

This is particularly useful when organizing all your worksheets relating to a particular period or year, for example.

Right click a tab
Select Tab Colour
And choose your favourite colour!

View all Excel hints and tips


Server loaded in 0.07 secs.