A family of Microsoft spreadsheet software with tools for analyzing, charting, and communicating data.
Jonathan_JA,
this assumes you data starts in row 2 with headers in row 1 so that the first header is in A1 and that you have headers out to the last column in row 1.
it adds a sheet on the end of the tab order for the duplicate rows.
make the sheet you want to process the activesheet before you run the macro. So you can run this recursively.
Test is on a copy of your workbook to be sure it does what you want.
Sub abc()
Dim sCol As String, sh As Worksheet
Dim r As Range, cell As Range, lastColumn As Long
sCol = "E" ' column with account number
Set sh = ActiveSheet
Set r = sh.Range(sCol & "2", sh.Cells(sh.Rows.Count, sCol).End(xlUp))
lcol = sh.Cells(1, sh.Columns.Count).End(xlToLeft).Column
Set r1 = r.Offset(0, lcol - r.Column + 1)
sform = "$" & sCol & "$" & 2 & ":" & "$" & sCol & 2
r1.Formula = "=Countif(" & sform & ",$" & sCol & 2 & ")"
r1.Formula = r1.Value
sh.Cells(1, r1.Column).Value = "HeaderZZZ"
On Error Resume Next
sh.ShowAllData
On Error GoTo 0
sh.Copy after:=Worksheets(Worksheets.Count)
Set sh1 = ActiveSheet
sh.Range("A1").CurrentRegion.AutoFilter Field:=r1.Column, Criteria1:="<>1"
Set r2 = sh.AutoFilter.Range
Set r2 = r2.Offset(1, 0).Resize(, 1)
Set r4 = sh1.Range(r2.Address)
sh1.Range("A1").CurrentRegion.AutoFilter Field:=r1.Column, Criteria1:="=1"
On Error Resume Next
Set r3 = r2.SpecialCells(xlVisible)
r3.EntireRow.Delete
Set r3 = Nothing
Set r3 = r4.SpecialCells(xlVisible)
Application.Goto r3
r3.EntireRow.Delete
sh.AutoFilterMode = False
sh1.AutoFilterMode = False
sh1.Columns(r1.Column).EntireColumn.Delete
r1.EntireColumn.Delete
On Error GoTo 0
End Sub
--
Regards,
Tom Ogilvy