+ Reply to Thread
Results 1 to 6 of 6

VBA code To Merge All .csv files into a single excel sheet

  1. #1
    Registered User
    Join Date
    06-03-2012
    Location
    Malaysia
    MS-Off Ver
    Excel 2007
    Posts
    4

    VBA code To Merge All .csv files into a single excel sheet

    Hi All VBA experts:
    I have 1000+ data files stored in csv. All of these files have the same structure& format.
    I use a vba code written by abousetta

    To merge it into one spreadsheet automatically, but the problem is,the data i needed start only from row 11 onward.
    Is there a way to modify the code to allow this to happen?

    Here's my requirement on the code:
    1) Must be able to merge all the file csv file in on shoot,
    2) Data needed start from 11th row onward,
    3) after open each file, copy the data from 11th row and onward, copy and paste it into MasterCSV sheet, the next data will be paste to the same column but next row to the previous data copied...

    I attach one of my file into the thread...



    Option Explicit

    Sub CombineCSVFiles()

    Dim sCSV$, sCombCSV$, iFF%
    Dim myFolder, myFile, arrCSV
    Dim myRange As Range
    Dim fso As Object
    Dim fPath As String

    ' Turn off some Excel functionality so your code runs faster
    With Application
    .ScreenUpdating = False
    .DisplayStatusBar = False
    .EnableEvents = False
    .Calculation = xlCalculationManual
    End With

    ' Use File System Object to choose folder with files
    Set fso = CreateObject("Scripting.FileSystemObject")

    With Application.FileDialog(msoFileDialogFolderPicker)
    .AllowMultiSelect = False
    .Show
    .InitialFileName = ThisWorkbook.Path & "\" ' Default path - Change as required
    .Title = "Please Select a Folder"
    .ButtonName = "Select Folder"
    If .SelectedItems.Count > 0 Then fPath = .SelectedItems(1) & "\" Else Exit Sub
    End With

    ' Open each file consequently and merge into a variable
    iFF = FreeFile
    Set myFolder = fso.GetFolder(fPath).Files
    For Each myFile In myFolder
    If LCase(myFile) Like "*.csv" Then
    sCSV = Space(FileLen(myFile))
    Open myFile For Binary Access Read As #iFF
    Get #iFF, , sCSV
    sCombCSV = sCombCSV & sCSV
    Close #iFF
    End If
    ' Loop through all files in folder
    Next myFile

    ' Convert variable to array
    arrCSV = Split(sCombCSV, vbCrLf)

    ' Paste data back to Excel
    Set myRange = Range(Range("A" & Range("A" & Rows.Count).End(xlUp).Row + 1), Range("A" & Rows.Count).End(xlUp).Offset(UBound(arrCSV) + 1))
    myRange = Application.Transpose(arrCSV)
    myRange.TextToColumns DataType:=xlDelimited, ConsecutiveDelimiter:=False, Comma:=True

    ' clean up
    myFile = vbNullString
    iFF = 1

    ' Turn Excel functionality back on
    With Application
    .DisplayStatusBar = True
    .EnableEvents = True
    .Calculation = xlCalculationAutomatic
    End With

    End Sub

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: VBA code To Merge All .csv files into a single excel sheet

    'CSVs To One Sheet (Stacked)
    A macro for importing all CSV files in a folder into a single Excel sheet, each CSV filename will be listed on the sheet next to the data that it came from.
    CSVs to 1 Sheet - Part 1



    The edit you would need to get it copy from row11 downward would be:

    Please Login or Register  to view this content.
    ...change that to:

    Please Login or Register  to view this content.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Valued Forum Contributor
    Join Date
    05-21-2009
    Location
    Great Britain
    MS-Off Ver
    Excel 2003
    Posts
    550

    Re: VBA code To Merge All .csv files into a single excel sheet

    Try this:
    Please Login or Register  to view this content.
    Post responsibly. Search for excelforum.com

  4. #4
    Registered User
    Join Date
    06-03-2012
    Location
    Malaysia
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: VBA code To Merge All .csv files into a single excel sheet

    Hi JBeaucaire ;
    Your Code is a great solution;
    it work; It did copy start from 11th row as i needed...

    By the way; the " file name " that was listed on the sheet next to the all the imported data;
    Something strange happen;
    It didnt insert the complete name of the my data file.

    here i attached the excel file that i use with your code; and an example of my actual data file imported.
    Data FileName: 120502141657_PHC4#_A_PHC4G_A_IBE001_0785_Shallow.csv
    120503215613_PJ25N_A_P5K2#_C_IBE001_0824_Deep.csv

    instead;
    it copied only
    120502141657_PHC4#_A_PHC4G_A_IBE0
    120503215613_PJ25N_A_P5K2#_C_IBE0

  5. #5
    Registered User
    Join Date
    06-03-2012
    Location
    Malaysia
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: VBA code To Merge All .csv files into a single excel sheet

    i uploaded the one of the example for data file that I have attached in the following attachment

  6. #6
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: VBA code To Merge All .csv files into a single excel sheet

    You are correct, the "trick" for putting the name of the CSV file in column A is actually using the sheet NAME created when the CSV is opened, and sheetnames are limited to 29 characters (or so), so that's what you're observing.

    If you really need the whole filename, though, easy fix, change this:

    Please Login or Register  to view this content.

    ...to this:
    Please Login or Register  to view this content.

    I may change that on my website... still thinking it over.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1