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

delete rows

ResolvedVersion 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:

Convert Text to Columns in Excel 2010

If you have a cell in your Excel spreadsheet that contains a lot of text and you want to divide it into separate columns, this can only be done if there is a logical character which separates the text, for example, a comma.

Select the cells you would like to convert. On the Data tab, click Text to Columns. Choose the format of your current data.

Select Delimited if the text contains a logical character otherwise select Fixed Width if there are a certain number of spaces between each field.

Click Next when a preview of the data appears. Then select the type of character that separates the various fields. If the character is not listed, select Other and enter the character.

Click Next again and then choose the format for each of the columns. Select the column heading in the Data preview and then select a data type from the Column data format options.

Click Finish and the text will appear in several columns.

View all Excel hints and tips

Connect with us:

0207 987 3777

Call for assistance

Request Callback

We will call you back

Server loaded in 0.09 secs.