PC Review


Reply
Thread Tools Rate Thread

Delete rows based on results of cell tests

 
 
lousyiain@gmail.com
Guest
Posts: n/a
 
      31st Mar 2009
Hi

I have ~200 rows of data - this data is split into groups of 5-12
rows.
At the bottom of each of these groups is a total

I am trying to find the rows with the word "total" and test if the
value one cell to the right=0
if the total=0 then i want to delete all the rows between this total
and the next total which does not equal 0

I fairly amateurish with my VBA but below is what I have so far
(probably completely wrong)
A sample of what the data might look is at the bottom

Thanks for your help
Iain


Sub ZeroChunkDelete

Dim lastrow As Long
Dim n As Long
Dim m As Long
Dim BottomZeroTotal As Long
Dim TopZeroTotal As Long

lastrow = Sheets("Print Out").Cells(Cells.Rows.Count, "B").End
(xlDown).Row
For n = lastrow To 1 Step -1
If UCase(Cells(n, 1).Value) = "Total" And Selection.End
(xlToRight).Value = 0 Then BottomZeroTotal = n
Next n

For m = n To 1 Step -1
If UCase(Cells(m, 1).Value) = "Total" And Selection.End
(xlToRight).Value = 0 Then TopZeroTotal = m
Next m
Worksheets("Print Out").Rows(TopZeroTotal & ":" &
BottomZeroTotal).Delete
End Sub


Data sample
A B
2
0
1
total 3
}
0 }
0 }I want to delete these rows
0 }
total 0 }

1
0
total 1



 
Reply With Quote
 
 
 
 
joel
Guest
Posts: n/a
 
      31st Mar 2009
Make sure there are no blank characters in the cell with the row total. I
improved your code. My code is fully tested. One problem I say with your
code that was wrong is shown below

from
If UCase(Cells(m, 1).Value) = "Total" 'this would never matc h
to
If UCase(Cells(m, 1).Value) = "TOTAL"



Sub ZeroChunkDelete()

Dim LastRow As Long
Dim RowCount As Long
Dim EndRow As Long
Dim Remove As Boolean

LastRow = Sheets("Print Out").Range("B" & Rows.Count).End(xlUp).Row
Remove = False
For RowCount = LastRow To 1 Step -1
If UCase(Range("A" & RowCount)) = "TOTAL" Then
If Remove = True Then
Rows((RowCount + 1) & ":" & EndRow).Delete
End If
If Range("B" & RowCount).Value = 0 Then
EndRow = RowCount
Remove = True
Else
Remove = False
End If
End If
Next RowCount
If Remove = True Then
Rows("1:" & EndRow).Delete
End If

End Sub



"(E-Mail Removed)" wrote:

> Hi
>
> I have ~200 rows of data - this data is split into groups of 5-12
> rows.
> At the bottom of each of these groups is a total
>
> I am trying to find the rows with the word "total" and test if the
> value one cell to the right=0
> if the total=0 then i want to delete all the rows between this total
> and the next total which does not equal 0
>
> I fairly amateurish with my VBA but below is what I have so far
> (probably completely wrong)
> A sample of what the data might look is at the bottom
>
> Thanks for your help
> Iain
>
>
> Sub ZeroChunkDelete
>
> Dim lastrow As Long
> Dim n As Long
> Dim m As Long
> Dim BottomZeroTotal As Long
> Dim TopZeroTotal As Long
>
> lastrow = Sheets("Print Out").Cells(Cells.Rows.Count, "B").End
> (xlDown).Row
> For n = lastrow To 1 Step -1
> If UCase(Cells(n, 1).Value) = "Total" And Selection.End
> (xlToRight).Value = 0 Then BottomZeroTotal = n
> Next n
>
> For m = n To 1 Step -1
> If UCase(Cells(m, 1).Value) = "Total" And Selection.End
> (xlToRight).Value = 0 Then TopZeroTotal = m
> Next m
> Worksheets("Print Out").Rows(TopZeroTotal & ":" &
> BottomZeroTotal).Delete
> End Sub
>
>
> Data sample
> A B
> 2
> 0
> 1
> total 3
> }
> 0 }
> 0 }I want to delete these rows
> 0 }
> total 0 }
>
> 1
> 0
> total 1
>
>
>
>

 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Delete Rows based on cell value BrianWest83@gmail.com Microsoft Excel Programming 7 18th Jan 2008 04:29 AM
Delete Rows based on cell values. Little Penny Microsoft Excel Programming 2 25th Sep 2007 02:23 PM
Delete duplicate rows based on part of cell. okrob Microsoft Excel Programming 0 14th Feb 2007 07:25 PM
Delete rows based on a cell value in the row =?Utf-8?B?RGF2ZQ==?= Microsoft Excel Discussion 7 14th Mar 2006 06:44 PM
Delete rows based on Cell name gmunro Microsoft Excel Programming 1 25th Nov 2005 01:09 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:10 PM.