1

I need to combine hundreds of csv files with the same format to one without having duplicate headers using an excel makro. I can select the files I want to import (and import them) with the following code:

Dim dateien, i, lastrow
lastrow = 1
dateien = Application.GetOpenFilename _
("csv-Dateien (*.csv), *.csv", MultiSelect:=True)

If IsArray(dateien) Then
    For i = 1 To UBound(dateien)
        Workbooks.Open dateien(i), local:=True
        With ThisWorkbook.Sheets(1)
            ActiveSheet.UsedRange.Copy Destination:=.Range("A" & lastrow)
            lastrow = .UsedRange.Rows.Count + 1
        End With
        ActiveWorkbook.Close False
    Next i
End If

However i don't really have a clue how to remove the duplicate headers...

1 Answer 1

1

I'd take the approach of opening each file in a FileSystemObject, reading all its data in and then firing it back out without the headers myself:

Dim dateien, i, lastrow
lastrow = 1
dateien = Application.GetOpenFilename _
("csv-Dateien (*.csv), *.csv", MultiSelect:=True)

dim oFso : Set oFso = CreateObject("Scripting.FileSystemObject")
Dim oSourceFile, oTargetFile
Set oTargetFile = oFso.CreateTextFile("pathtofilehere", True)
Dim sArray()

If IsArray(dateien) Then
    For i = 1 To UBound(dateien) ' Arrays in VBA index from zero, not 1 - you're skipping the first element in dateien
        ReDim sArray(0)
        Set oSourceFile = oFso.OpenTextFile(dateien(i), 1) ' open for reading
        While Not oSourceFile.AtEndOfStream ' while there is data in the file
            sArray(Ubound(sArray)) = oSourceFile.ReadLine  ' add the line to an array
            ReDim Preserve sArray(UBound(sArray)+1) ' increase size of the array by 1
        Wend
        ' Now we have the whole file in an array
        For myLoop = 1 to UBound(sArray) ' Loop from 1 and we skip the header line in the file
            oTargetFile.WriteLine sArray(myLoop) ' write array values into file
        Next myLoop   ' repeat for each line in the array
    Next i    ' repeat for each file in dateien
End If
Sign up to request clarification or add additional context in comments.

8 Comments

Seems to be a good idea, however I get a compilation error at ReDim Preserve sArray(UBound(sArray)) (Array Already Dimensioned); If I declare sArray (), I get the runtime error Subscript out of range (Error 9) at sArray(UBound(sArray)) = oSourceFile.ReadLine
Oops. Fixed in code now... The trick is to declare it as dynamic initially with Dim sArray() then redimension it so it has the expected upper boundary for the first element with a ReDim
I still get Error 9 (Subscript out of range) for Set oSourceFile = oFso.OpenTextFile(dateien(i), 1) :(
Having checked with the code directly, it seems that the array returned by Application.GetOpenFileName is not zero indexed, rather indexed from 1. I've modified the answer; should work now. Let me know if there's any issues
I've moved the ReDim sArray(0) statement so that it resets for each file; previously it was always appending the other file's data to the original array. This will clear it out for each file and should properly resolve your header issue
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.