Solved

remove duplicate headers

Posted on 2014-09-11
10
201 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
Comment Utility
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 48

Expert Comment

by:Rgonzo1971
Comment Utility
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
Comment Utility
Hi Rgonzo, that ran but didn't actually do anything ?
0
 
LVL 48

Expert Comment

by:Rgonzo1971
Comment Utility
Pls send a dummy
0
 

Author Comment

by:Jagwarman
Comment Utility
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 48

Accepted Solution

by:
Rgonzo1971 earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
The remove headers is perfect thanks
0
 
LVL 48

Expert Comment

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

Author Comment

by:Jagwarman
Comment Utility
ok tks
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Sparklines have been introduced with Excel 2010 and are a useful tool for creating small in-cell charts, used for example in dashboards. Excel 2010 offers three different types of Sparklines: Line, Column and Win/Loss. What it does not offer is a…
Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

763 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

8 Experts available now in Live!

Get 1:1 Help Now