PC Review


Reply
Thread Tools Rate Thread

Delete Duplicate Header Macro

 
 
Workbook
Guest
Posts: n/a
 
      20th Feb 2009
Row A1:L1 contain the following contents.
A1 = OT
B1 = ON
C1 = PT
D1 = M
E1 = SN
F1 = AD&T
H1 = RQ
I1 = PN
J1 = D
K1 = P
L1 = SL
I would like to search A2:L100 for these contents and every time I find them
in a row to have that row be deleted. Any input is greatly appreciated.
 
Reply With Quote
 
 
 
 
Joel
Guest
Posts: n/a
 
      20th Feb 2009
I'm not sure if it was worth the effort to use Find/FindNext because of all
the problems I ran into. I show you tow methods and you decide which is
better



Sub DeleteRows()

With ActiveSheet
LastCol = .Cells(1, Columns.Count).End(xlToLeft).Column
LastRow = .Range("A" & Rows.Count).End(xlUp).Row

Set DataRange = .Range("A1", .Cells(LastRow, LastCol))
For ColCount = 1 To LastCol
Header = .Cells(1, ColCount)
Set HeaderCell = .Cells(1, ColCount)
Set c = DataRange.Find(what:=Header, LookIn:=xlValues,
SearchOrder:=xlByRows, _
after:=Cells(1, LastCol))
If Not c Is Nothing And c.Row <> 1 Then
Do
Set EndRow = Cells(c.Row - 1, LastCol)
c.EntireRow.Delete
Set c = DataRange.Find(what:=Header, after:=EndRow, _
LookIn:=xlValues, SearchOrder:=xlByRows)
Loop While Not c Is Nothing And c.Row <> 1
End If

Next ColCount
End With


End Sub

or

Sub DeleteRows2()

With ActiveSheet
LastCol = .Cells(1, Columns.Count).End(xlToLeft).Column
LastRow = .Range("A" & Rows.Count).End(xlUp).Row
Set Headers = .Range("A1", .Cells(1, LastCol))

RowCount = LastRow
Do While RowCount >= 2

For Each Header In Headers
Set RowCells = .Range(.Cells(RowCount, "A"), .Cells(RowCount,
LastCol))
Set c = RowCells.Find(what:=Header, LookIn:=xlValues)
If Not c Is Nothing Then
Rows(RowCount).Delete
Exit For
End If
Next Header
RowCount = RowCount - 1
Loop
End With

End Sub


"Workbook" wrote:

> Row A1:L1 contain the following contents.
> A1 = OT
> B1 = ON
> C1 = PT
> D1 = M
> E1 = SN
> F1 = AD&T
> H1 = RQ
> I1 = PN
> J1 = D
> K1 = P
> L1 = SL
> I would like to search A2:L100 for these contents and every time I find them
> in a row to have that row be deleted. Any input is greatly appreciated.

 
Reply With Quote
 
Dave Peterson
Guest
Posts: n/a
 
      20th Feb 2009
Option Explicit
Sub testme()

Dim iRow As Long
Dim FirstRow As Long
Dim LastRow As Long

Dim iCol As Long
Dim FirstCol As Long
Dim LastCol As Long
Dim FoundADifference As Boolean

Dim wks As Worksheet

Set wks = Worksheets("Sheet1")

With wks
FirstRow = 2
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row

FirstCol = .Range("A1").Column
LastCol = .Range("L1").Column

'start at bottom and work up
For iRow = LastRow To FirstRow Step -1
FoundADifference = False
For iCol = FirstCol To LastCol
If .Cells(iRow, iCol).Value = .Cells(1, iCol).Value Then
'keep looking
Else
FoundADifference = True
Exit For 'stop looking
End If
Next iCol
If FoundADifference = True Then
'keep it
Else
.Rows(iRow).Delete
End If
Next iRow
End With

End Sub


Workbook wrote:
>
> Row A1:L1 contain the following contents.
> A1 = OT
> B1 = ON
> C1 = PT
> D1 = M
> E1 = SN
> F1 = AD&T
> H1 = RQ
> I1 = PN
> J1 = D
> K1 = P
> L1 = SL
> I would like to search A2:L100 for these contents and every time I find them
> in a row to have that row be deleted. Any input is greatly appreciated.


--

Dave Peterson
 
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
Macro to Delete Duplicate Cells MCRH Microsoft Excel Programming 2 23rd Mar 2010 08:43 PM
Find duplicate, save in a list, delete duplicate using macro Erica Microsoft Excel Programming 1 27th Jan 2010 06:53 PM
Macro to delete duplicate records =?Utf-8?B?SG9yc2Vsb3Zlcg==?= Microsoft Excel Programming 2 1st Jul 2004 03:06 PM
Re: Macro to delete duplicate data mudraker Microsoft Excel Programming 0 14th Nov 2003 12:00 AM
Macro to delete duplicate data in row Tony Microsoft Excel Programming 2 13th Nov 2003 03:17 PM


Features
 

Advertising
 

Newsgroups
 


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