PC Review


Reply
Thread Tools Rate Thread

Deleting Duplicate Rows In a Selection with a True Statement

 
 
Josh heep
Guest
Posts: n/a
 
      12th Feb 2008
Hey,

I was wondering if there is a more efficient way to write this macro.
It searches through a selected range and deletes any row that is
valued as True. I made it to remove duplicates based on various
criteria that I change from project to project. Thanks for any
insight!

Sub Delete_True_Rows_In_Selection()

Dim TotalRows As Integer
Dim FirstRow As Integer
Dim Col As Integer
Dim Row As Integer

TotalRows = Selection.Rows.Count
FirstRow = Selection.Row()
Col = Selection.Column()

Application.ScreenUpdating = False

On Error Resume Next
For Row = FirstRow To TotalRows
If Cells(Row, Col).Value = True Then
Rows(Row).Delete
Row = Row - 1
End If
Next Row

Application.ScreenUpdating = True

End Sub
 
Reply With Quote
 
 
 
 
Bernie Deitrick
Guest
Posts: n/a
 
      13th Feb 2008
Josh,

The fastest way is to sort, autofilter based on the deletion criteria
column, select visible cells, and then delete rows. No looping involved.

This macro assumes that there is a header row, no blank columns or rows,
and that the cells with True/False are selected prior to running (but the
header
row cell is NOT selected).

HTH,
Bernie
MS Excel MVP


Sub Delete_True_Rows_In_Selection2()

Dim myR As Range
Dim myS As Range

Set myS = Selection
Set myR = ActiveCell.CurrentRegion
myR.Sort Key1:=myS.Cells(1), Order1:=xlAscending, Header:=xlYes
myR.AutoFilter Field:=myS.Column - myR(1).Column + 1, Criteria1:="TRUE"
myS.SpecialCells(xlCellTypeVisible).EntireRow.Delete
myS.AutoFilter
End Sub


"Josh heep" <(E-Mail Removed)> wrote in message
news:5f1936cd-a424-45e7-b5b4-(E-Mail Removed)...
> Hey,
>
> I was wondering if there is a more efficient way to write this macro.
> It searches through a selected range and deletes any row that is
> valued as True. I made it to remove duplicates based on various
> criteria that I change from project to project. Thanks for any
> insight!
>
> Sub Delete_True_Rows_In_Selection()
>
> Dim TotalRows As Integer
> Dim FirstRow As Integer
> Dim Col As Integer
> Dim Row As Integer
>
> TotalRows = Selection.Rows.Count
> FirstRow = Selection.Row()
> Col = Selection.Column()
>
> Application.ScreenUpdating = False
>
> On Error Resume Next
> For Row = FirstRow To TotalRows
> If Cells(Row, Col).Value = True Then
> Rows(Row).Delete
> Row = Row - 1
> End If
> Next Row
>
> Application.ScreenUpdating = True
>
> End Sub



 
Reply With Quote
 
Josh heep
Guest
Posts: n/a
 
      13th Feb 2008
Thanks for the help Bernie worked great after I removed some merged
cells I had.

On Feb 12, 6:20 pm, "Bernie Deitrick" <deitbe @ consumer dot org>
wrote:
> Josh,
>
> The fastest way is to sort, autofilter based on the deletion criteria
> column, select visible cells, and then delete rows. No looping involved.
>
> This macro assumes that there is a header row, no blank columns or rows,
> and that the cells with True/False are selected prior to running (but the
> header
> row cell is NOT selected).
>
> HTH,
> Bernie
> MS Excel MVP
>
> Sub Delete_True_Rows_In_Selection2()
>
> Dim myR As Range
> Dim myS As Range
>
> Set myS = Selection
> Set myR = ActiveCell.CurrentRegion
> myR.Sort Key1:=myS.Cells(1), Order1:=xlAscending, Header:=xlYes
> myR.AutoFilter Field:=myS.Column - myR(1).Column + 1, Criteria1:="TRUE"
> myS.SpecialCells(xlCellTypeVisible).EntireRow.Delete
> myS.AutoFilter
> End Sub
>
> "Josh heep" <jph...@gmail.com> wrote in message
>
> news:5f1936cd-a424-45e7-b5b4-(E-Mail Removed)...
>
> > Hey,

>
> > I was wondering if there is a more efficient way to write this macro.
> > It searches through a selected range and deletes any row that is
> > valued as True. I made it to remove duplicates based on various
> > criteria that I change from project to project. Thanks for any
> > insight!

>
> > Sub Delete_True_Rows_In_Selection()

>
> > Dim TotalRows As Integer
> > Dim FirstRow As Integer
> > Dim Col As Integer
> > Dim Row As Integer

>
> > TotalRows = Selection.Rows.Count
> > FirstRow = Selection.Row()
> > Col = Selection.Column()

>
> > Application.ScreenUpdating = False

>
> > On Error Resume Next
> > For Row = FirstRow To TotalRows
> > If Cells(Row, Col).Value = True Then
> > Rows(Row).Delete
> > Row = Row - 1
> > End If
> > Next Row

>
> > Application.ScreenUpdating = True

>
> > End Sub


 
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
VB Help If Statement in Selection.ShowDetail = True TroyT Microsoft Excel Programming 5 22nd May 2008 07:09 PM
Deleting duplicate rows =?Utf-8?B?QmFsYW4=?= Microsoft Excel Programming 3 26th Aug 2007 04:04 AM
deleting mulitple rows when the checkbox value = true. =?Utf-8?B?YnVyZ2VlZQ==?= Microsoft Excel Misc 1 30th Jan 2007 04:04 PM
Deleting Non-Duplicate Rows BillW Microsoft Excel Discussion 1 23rd Sep 2005 07:12 PM
Deleting duplicate rows.....there's more Fredy Microsoft Excel Worksheet Functions 1 24th Jun 2004 06:48 PM


Features
 

Advertising
 

Newsgroups
 


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