Solved

remove duplicate headers

Posted on 2014-09-11
10
207 Views
Last Modified: 2014-09-11
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
0
Comment
Question by:Jagwarman
  • 5
  • 4
10 Comments
 
LVL 68

Expert Comment

by:Qlemo
ID: 40317004
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
 
LVL 49

Expert Comment

by:Rgonzo1971
ID: 40317027
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
 

Author Comment

by:Jagwarman
ID: 40317098
Hi Rgonzo, that ran but didn't actually do anything ?
0
 
LVL 49

Expert Comment

by:Rgonzo1971
ID: 40317108
Pls send a dummy
0
 

Author Comment

by:Jagwarman
ID: 40317183
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 49

Accepted Solution

by:
Rgonzo1971 earned 500 total points
ID: 40317197
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
 

Author Comment

by:Jagwarman
ID: 40317226
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
 

Author Comment

by:Jagwarman
ID: 40317232
The remove headers is perfect thanks
0
 
LVL 49

Expert Comment

by:Rgonzo1971
ID: 40317244
if you try to copy from a XLSM or XLSX file to a XLS this problem will appear
0
 

Author Comment

by:Jagwarman
ID: 40317927
ok tks
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

912 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

27 Experts available now in Live!

Get 1:1 Help Now