Solved

remove duplicate headers

Posted on 2014-09-11
10
246 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 69

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 50

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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 50

Expert Comment

by:Rgonzo1971
ID: 40317108
Pls send a dummy
0
 

Author Comment

by:Jagwarman
ID: 40317183
0
 
LVL 50

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 50

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
nested If/and formula needed 12 64
using vb script 5 51
Modification to nested formula needed 2 25
Excel 2013 Find and copy duplicates to a new worksheet 6 36
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
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.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.

756 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