Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 41
Default Delete rows if fits criteria in a Macro

Hi

I currently have a macro I'm working on in which there are 12 worksheets
(January to December).

In each spreadsheet I want to delete all rows that contain "0" in a
particular column but keep all the others.

This is the current code I'm using:

Columns("E:E").Select
Selection.NumberFormat = "0"

For Each c In Range("E1:E200")
If c = 0 Then Rows(c.Row).Delete
Next c

In the 12 worksheets, it deletes all but 1 or 2 of the rows I want gone.

How do I fix it so that all the rows are deleted?

Thanks in advance for your help.
Forest

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,501
Default Delete rows if fits criteria in a Macro

Hi,

If you work forward through a range deleting as you go then if 2
consecutive rows match your criterial you'll miss the second. Try this which
loops through all your worksheets and block deletes those rows that meet your
criteria

Sub sonic()
Dim MyRange1 As Range
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
For Each c In ws.Range("E1:E200")
If c.Value < "" And c.Value = 0 Then
If MyRange1 Is Nothing Then
Set MyRange1 = c.EntireRow
Else
Set MyRange1 = Union(MyRange1, c.EntireRow)
End If
End If
Next c
If Not MyRange1 Is Nothing Then
MyRange1.Delete
End If
Set MyRange1 = Nothing
Next ws
End Sub

Mike

"forest8" wrote:

Hi

I currently have a macro I'm working on in which there are 12 worksheets
(January to December).

In each spreadsheet I want to delete all rows that contain "0" in a
particular column but keep all the others.

This is the current code I'm using:

Columns("E:E").Select
Selection.NumberFormat = "0"

For Each c In Range("E1:E200")
If c = 0 Then Rows(c.Row).Delete
Next c

In the 12 worksheets, it deletes all but 1 or 2 of the rows I want gone.

How do I fix it so that all the rows are deleted?

Thanks in advance for your help.
Forest

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,986
Default Delete rows if fits criteria in a Macro

When deleting rows, you should start at the bottom of the column and work
upward. This avoids the row skips that occur when deleting from top down,
due to the default shift up after deletion.

Change this:
For Each c In Range("E1:E200")
If c = 0 Then Rows(c.Row).Delete
Next c

To:
For i = 200 To 1 Step -1
If Range("E" & i) = 0 Then
Rows(i).Delete
End If
Next



"forest8" wrote:

Hi

I currently have a macro I'm working on in which there are 12 worksheets
(January to December).

In each spreadsheet I want to delete all rows that contain "0" in a
particular column but keep all the others.

This is the current code I'm using:

Columns("E:E").Select
Selection.NumberFormat = "0"

For Each c In Range("E1:E200")
If c = 0 Then Rows(c.Row).Delete
Next c

In the 12 worksheets, it deletes all but 1 or 2 of the rows I want gone.

How do I fix it so that all the rows are deleted?

Thanks in advance for your help.
Forest

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 41
Default Delete rows if fits criteria in a Macro

Hi

The second solution was very helpful for me. Unfortunately, the first
solution did not solve my issue.

Thanks for the help.
J

"JLGWhiz" wrote:

When deleting rows, you should start at the bottom of the column and work
upward. This avoids the row skips that occur when deleting from top down,
due to the default shift up after deletion.

Change this:
For Each c In Range("E1:E200")
If c = 0 Then Rows(c.Row).Delete
Next c

To:
For i = 200 To 1 Step -1
If Range("E" & i) = 0 Then
Rows(i).Delete
End If
Next



"forest8" wrote:

Hi

I currently have a macro I'm working on in which there are 12 worksheets
(January to December).

In each spreadsheet I want to delete all rows that contain "0" in a
particular column but keep all the others.

This is the current code I'm using:

Columns("E:E").Select
Selection.NumberFormat = "0"

For Each c In Range("E1:E200")
If c = 0 Then Rows(c.Row).Delete
Next c

In the 12 worksheets, it deletes all but 1 or 2 of the rows I want gone.

How do I fix it so that all the rows are deleted?

Thanks in advance for your help.
Forest

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
Create a Macro to Delete All Rows that meet a certain criteria jpittari Excel Programming 1 November 1st 07 05:16 AM
Macro to Delete rows on a criteria [email protected] Excel Programming 2 August 8th 07 08:37 PM
Formula/Macro to delete rows that do not meet criteria from a list? S Davis Excel Worksheet Functions 2 July 12th 06 07:42 PM
Macro, delete rows that meet criteria Scott Wagner Excel Programming 4 December 23rd 05 12:06 AM
Cut and Paste macro based on criteria then delete empty rows samst Excel Programming 4 November 2nd 03 09:33 PM


All times are GMT +1. The time now is 01:20 PM.

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"