delete rows
The UK's Number 1 for Microsoft Office Training Add this page to your favourites/bookmarksBookmark page
 
View printable version of pagePrintable version
Plus One Google
Customer: Sign in
Delegate: Sign in
Trainer: Log in

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

Delete rows

resolvedResolved · High Priority · Version 2010

replyReply Mon 6 Feb 2012, 12:06Delegate Ross said...

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

For upcoming training course dates see: Pricing & availability

replyReply Mon 6 Feb 2012, 15:37Trainer Doug said...

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

 

Please browse our web site to find out more about
application development excel and other Microsoft training courses.

Excel tip:

Ctrl+d's double life

Suppose I have a formula in B1 that I wish to copy into B2:B10. I can select B1:B10 then press Ctrl+d to copy the formula down the selected range. Users generally ignore this shortcut in favour of double-clicking on the fill handle to copy down, but Ctrl+d is useful sometimes particularly when there is no data in surrounding columns to guide to how far the double-click method should copy formulae.

Ctrl+d has another use though. When I use the drawing toolbar to draw objects such as Text Boxes, Rectangles and Ovals onto a worksheet, Ctrl+d makes an instant duplicate of selected shapes. For example, I need five Text Boxes the same size. I draw one Text box and adjust it to the size I want, select it, then press Ctrl+d four times to get four identical copies.

View all Excel hints and tips


Microsoft Certified Partner Accredited Training Provider: Institute of IT Training Institute of Leadership and Management - Certified Courses Security Seal verified by visa, mastercard securecard