#1   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 20
Default Loop & Delete

Hii guys

Please help below

I need a procedure that will loop down column B, and delete any row with the
first five letters = "Total" and the next row.


Thanks
  #2   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 2,345
Default Loop & Delete

Jenna,

You should loop UP through the rows not down otherwise the count of the loop
will go wrong.

Try something like:

Sub DeleteIt()
Dim Starter
Dim c As Long


Starter = Cells(Rows.Count, 1).End(xlUp).Row

For c = Starter To 1 Step -1
'Change the 1 in Cells(c, 1) to the number of the column that yo want to
check
If Left(Cells(c, 1).Value, 5) = "Total" Then
Range(Cells(c, 1), Cells(c + 1, 1)).EntireRow.Delete
End If
Next c

End Sub

--
HTH

Sandy
In Perth, the ancient capital of Scotland
and the crowning place of kings


with @tiscali.co.uk


"jenna" wrote in message
...
Hii guys

Please help below

I need a procedure that will loop down column B, and delete any row with
the
first five letters = "Total" and the next row.


Thanks



  #3   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 20
Default Loop & Delete



Thanks so far

Do i have to place my cursor at the bottom of the column?
Also how do i delete the previous row too


thaks
  #4   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 2,345
Default Loop & Delete

"jenna" wrote in message
...

Do i have to place my cursor at the bottom of the column?

You don't have to place the cursor any where in particular

My apologies, I failed to notice that you asked the procedure to look at
Column B - I really should learn how to read!

Use the Macro:

Sub DeleteIt()
Dim Starter
Dim c As Long

Starter = Cells(Rows.Count, 2).End(xlUp).Row

For c = Starter To 1 Step -1
If Left(Cells(c, 2).Value, 5) = "Total" Then
Range(Cells(c, 2), Cells(c + 1, 2)).EntireRow.Delete
End If
Next c

End Sub


In this line:
Starter = Cells(Rows.Count, 2).End(xlUp).Row
the row number of the last filled cell in column 2 (ie Column B) is loaded
in the the variable *Starter*

this number is then used as the start of the loop
For c = Starter To 1 Step -1
which then steps UP through the column looking for a cell with *Total* in
it.

Also how do i delete the previous row too


You originally said:
first five letters = "Total" and the next row


the range in:
Range(Cells(c, 2), Cells(c + 1, 2)).EntireRow.Delete

is the row in which *Total* was found *and* the row below it
ie Cells(c, 2) is the cell in which *Total* was found and Cells(c + 1, 2) is
the next cell down (ie row c+1)

If you do want the *previous row* to be deleted then change the line then
post back again.
..

--
HTH

Sandy
In Perth, the ancient capital of Scotland
and the crowning place of kings


with @tiscali.co.uk


"jenna" wrote in message
...


Thanks so far

Do i have to place my cursor at the bottom of the column?
Also how do i delete the previous row too


thaks



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to Delete Rows in Excel In a Do Loop indraneel Excel Worksheet Functions 6 September 15th 06 09:51 AM
Adding a loop to conditional delete code maw via OfficeKB.com New Users to Excel 21 August 15th 06 04:11 PM
I can't delete a cell without deleting a row Tenny Excel Discussion (Misc queries) 2 June 30th 06 12:52 AM
Loop time seems dependent on unrelated workbook - Why? Richard Excel Worksheet Functions 2 March 30th 06 11:59 PM
Do Loop Noemi Excel Discussion (Misc queries) 0 December 8th 05 10:43 PM


All times are GMT +1. The time now is 05:07 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"