remove duplicate headers

after opening certain csv files there are sometimes many duplicate header rows. Is there a short quick piece of VBA that can remove all of the duplicate header but leave the main [row 1] header.

thanks
JagwarmanAsked:
Who is Participating?
 
Rgonzo1971Connect With a Mentor Commented:
The problem was that they weren't the same

"User-ID" became " User-ID"

Corrected Code

Sub macro()
Dim arrFirstRow As Variant
Dim arrNextRow As Variant
Set myRange = Range(Range("A1"), Range("A1").End(xlToRight))
arrFirstRow = myRange
strFirstRow = Join(WorksheetFunction.Index(arrFirstRow, 1, 0), ",")
LastRow = Range("A" & Rows.Count).End(xlUp).Row
For Idx = LastRow To 2 Step -1
    Set myRange = Range(Range("A" & Idx), Range("A" & Idx).End(xlToRight))
    arrNextRow = myRange
    strNextRow = Join(WorksheetFunction.Index(arrNextRow, 1, 0), ",")
    If strNextRow = strFirstRow Or strNextRow = " " & strFirstRow Then
        Cells(Idx, 1).EntireRow.Delete
    End If
Next

End Sub
0
 
QlemoDeveloperCommented:
You should never have "duplicate header rows" in CSV files. Is that because there is a word/line wrap in headers, maybe? So they build two instead of only one line? Anyway, that is far from any standard format.

There cannot be any generic code able to remove "duplicate header rows", because usually those rows would just look like data. You need special knowledge, like "first column cannot be a string", to check for superfluous lines.
0
 
Rgonzo1971Commented:
Hi,

pls try

Sub macro()
Dim arrFirstRow As Variant
Dim arrNextRow As Variant
Set myRange = Range(Range("A1"), Range("A1").End(xlToRight))
arrFirstRow = myRange
strFirstRow = Join(WorksheetFunction.Index(arrFirstRow, 1, 0), ",")
LastRow = Range("A" & Rows.Count).End(xlUp).Row
For Idx = LastRow To 2 Step -1
    Set myRange = Range(Range("A" & Idx), Range("A" & Idx).End(xlToRight))
    arrNextRow = myRange
    strNextRow = Join(WorksheetFunction.Index(arrNextRow, 1, 0), ",")
    If strNextRow = strFirstRow Then
        Cells(Idx, 1).EntireRow.Delete
    End If
Next

End Sub

Regards
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
JagwarmanAuthor Commented:
Hi Rgonzo, that ran but didn't actually do anything ?
0
 
Rgonzo1971Commented:
Pls send a dummy
0
 
JagwarmanAuthor Commented:
0
 
JagwarmanAuthor Commented:
Rgonzo you provided me with code back in dec to copy all tabs to a new workbook. I just tried to use it and it says "Can't insert the sheets into the destination workbook because it contains fewer rows and columns...... sure enough when I look at the new workbook it only has 64k rows even though I am opening it from Excel 2010 any ideas?
0
 
JagwarmanAuthor Commented:
The remove headers is perfect thanks
0
 
Rgonzo1971Commented:
if you try to copy from a XLSM or XLSX file to a XLS this problem will appear
0
 
JagwarmanAuthor Commented:
ok tks
0
All Courses

From novice to tech pro — start learning today.