PC Review


Reply
Thread Tools Rate Thread

Delete Rows help?

 
 
jaydywan@gmail.com
Guest
Posts: n/a
 
      27th Feb 2007
The following macro deletes all rows on the active worksheet
' that have 1034, 1035, 1037 column E. I am looking for someone to
let me know how to do the opposite, delete all rows EXCEPT that that
have 1034, 1035, 1037 column E. Thanks.

Sub Delete_Rows()
' This macro deletes all rows on the active worksheet
' that have 1034, 1035, 1037 column E.
Dim rng As Range, cell As Range, del As Range
Set rng = Intersect(Range("E:E"), ActiveSheet.UsedRange)
For Each cell In rng
If (cell.Value) = "1034" _
Or (cell.Value) = "1035" _
Or (cell.Value) = "1037" Then
If del Is Nothing Then
Set del = cell
Else: Set del = Union(del, cell)
End If
End If
Next
On Error Resume Next
del.EntireRow.Delete
End Sub

 
Reply With Quote
 
 
 
 
Bernie Deitrick
Guest
Posts: n/a
 
      27th Feb 2007
Jay,

Replace

If (cell.Value) = "1034" _
Or (cell.Value) = "1035" _
Or (cell.Value) = "1037" Then


with

If (cell.Value) <> "1034" _
And (cell.Value) <> "1035" _
And (cell.Value) <> "1037" Then


HTH,
Bernie
MS Excel MVP


<(E-Mail Removed)> wrote in message news:(E-Mail Removed)...
> The following macro deletes all rows on the active worksheet
> ' that have 1034, 1035, 1037 column E. I am looking for someone to
> let me know how to do the opposite, delete all rows EXCEPT that that
> have 1034, 1035, 1037 column E. Thanks.
>
> Sub Delete_Rows()
> ' This macro deletes all rows on the active worksheet
> ' that have 1034, 1035, 1037 column E.
> Dim rng As Range, cell As Range, del As Range
> Set rng = Intersect(Range("E:E"), ActiveSheet.UsedRange)
> For Each cell In rng
> If (cell.Value) = "1034" _
> Or (cell.Value) = "1035" _
> Or (cell.Value) = "1037" Then
> If del Is Nothing Then
> Set del = cell
> Else: Set del = Union(del, cell)
> End If
> End If
> Next
> On Error Resume Next
> del.EntireRow.Delete
> End Sub
>



 
Reply With Quote
 
marbarru@gmail.com
Guest
Posts: n/a
 
      27th Feb 2007
Using boolean logic this must be work...

If (cell.Value) <> "1034" and (cell.Value) <> "1035" and
(cell.Value) <> "1037" Then


On Feb 27, 1:08 pm, jaydy...@gmail.com wrote:
> The following macro deletes all rows on the active worksheet
> ' that have 1034, 1035, 1037 column E. I am looking for someone to
> let me know how to do the opposite, delete all rows EXCEPT that that
> have 1034, 1035, 1037 column E. Thanks.
>
> Sub Delete_Rows()
> ' This macro deletes all rows on the active worksheet
> ' that have 1034, 1035, 1037 column E.
> Dim rng As Range, cell As Range, del As Range
> Set rng = Intersect(Range("E:E"), ActiveSheet.UsedRange)
> For Each cell In rng
> If (cell.Value) = "1034" _
> Or (cell.Value) = "1035" _
> Or (cell.Value) = "1037" Then
> If del Is Nothing Then
> Set del = cell
> Else: Set del = Union(del, cell)
> End If
> End If
> Next
> On Error Resume Next
> del.EntireRow.Delete
> End Sub



 
Reply With Quote
 
Dave Peterson
Guest
Posts: n/a
 
      27th Feb 2007
Maybe just reversing the logic:

Sub Delete_Rows()
' This macro deletes all rows on the active worksheet
' that have 1034, 1035, 1037 column E.
Dim rng As Range, cell As Range, del As Range
Set rng = Intersect(Range("E:E"), ActiveSheet.UsedRange)
For Each cell In rng
If (cell.Value) = "1034" _
Or (cell.Value) = "1035" _
Or (cell.Value) = "1037" Then
'do nothing
else
If del Is Nothing Then
Set del = cell
Else
Set del = Union(del, cell)
End If
End If
Next cell

On Error Resume Next
del.EntireRow.Delete
on error goto 0
End Sub

I would think you'd want to check for numbers:

If (cell.Value) = 1034 _
Or (cell.Value) = 1035 _
Or (cell.Value) = 1037 Then
'do nothing
else
....


And sometimes, it's easier to use "select case" instead of a bunch of or's:
....
select case cell.value
case is = 1034, 1035, 1037
'do nothing
case else
If del Is Nothing Then
Set del = cell
Else
Set del = Union(del, cell)
End If
end select
....

It can make it easier to read/understand.


(E-Mail Removed) wrote:
>
> The following macro deletes all rows on the active worksheet
> ' that have 1034, 1035, 1037 column E. I am looking for someone to
> let me know how to do the opposite, delete all rows EXCEPT that that
> have 1034, 1035, 1037 column E. Thanks.
>
> Sub Delete_Rows()
> ' This macro deletes all rows on the active worksheet
> ' that have 1034, 1035, 1037 column E.
> Dim rng As Range, cell As Range, del As Range
> Set rng = Intersect(Range("E:E"), ActiveSheet.UsedRange)
> For Each cell In rng
> If (cell.Value) = "1034" _
> Or (cell.Value) = "1035" _
> Or (cell.Value) = "1037" Then
> If del Is Nothing Then
> Set del = cell
> Else: Set del = Union(del, cell)
> End If
> End If
> Next
> On Error Resume Next
> del.EntireRow.Delete
> End Sub


--

Dave Peterson
 
Reply With Quote
 
jaydywan@gmail.com
Guest
Posts: n/a
 
      27th Feb 2007
On Feb 27, 2:14 pm, "Bernie Deitrick" <deitbe @ consumer dot org>
wrote:
> Jay,
>
> Replace
>
> If (cell.Value) = "1034" _
> Or (cell.Value) = "1035" _
> Or (cell.Value) = "1037" Then
>
> with
>
> If (cell.Value) <> "1034" _
> And (cell.Value) <> "1035" _
> And (cell.Value) <> "1037" Then
>
> HTH,
> Bernie
> MS Excel MVP
>
> <jaydy...@gmail.com> wrote in messagenews:(E-Mail Removed)...
> > The following macro deletes all rows on the active worksheet
> > ' that have 1034, 1035, 1037 column E. I am looking for someone to
> > let me know how to do the opposite, delete all rows EXCEPT that that
> > have 1034, 1035, 1037 column E. Thanks.

>
> > Sub Delete_Rows()
> > ' This macro deletes all rows on the active worksheet
> > ' that have 1034, 1035, 1037 column E.
> > Dim rng As Range, cell As Range, del As Range
> > Set rng = Intersect(Range("E:E"), ActiveSheet.UsedRange)
> > For Each cell In rng
> > If (cell.Value) = "1034" _
> > Or (cell.Value) = "1035" _
> > Or (cell.Value) = "1037" Then
> > If del Is Nothing Then
> > Set del = cell
> > Else: Set del = Union(del, cell)
> > End If
> > End If
> > Next
> > On Error Resume Next
> > del.EntireRow.Delete
> > End Sub


thanks!

 
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
Autofil on variable rows, delete extract and show remaining rows 1plane Microsoft Excel Programming 3 17th Nov 2009 10:49 AM
Hpw do I delete multiple empty rows found between filled rows? Bill Microsoft Excel Worksheet Functions 1 15th Nov 2009 12:52 AM
Copy pasting Rows, but need to Delete any Shapes/Pictures that are within copied rows Corey Microsoft Excel Programming 2 1st Aug 2007 02:02 AM
Cut filtered rows, paste into next empty row of new sheet, and delete cut rows Scott Microsoft Excel Worksheet Functions 0 13th Dec 2006 01:25 AM
Delete rows with numeric values, leave rows with text =?Utf-8?B?R1NwbGluZQ==?= Microsoft Excel Programming 5 11th Oct 2005 12:44 AM


Features
 

Advertising
 

Newsgroups
 


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