+ Reply to Thread
Results 1 to 1 of 1

merge csv files within same folder into one master file

Hybrid View

  1. #1
    Registered User
    Join Date
    04-30-2009
    Location
    Spain
    MS-Off Ver
    Excel 2010
    Posts
    88

    merge csv files within same folder into one master file

    Hi all,

    I found this nice code that merges all files in a folder into one master file.

    Sub MergeFilesMultiSheetsPath1()
    Dim wbDst As Workbook
    Dim wbSrc As Workbook
    Dim wsSrc As Worksheet
    Dim MyPath As String
    Dim strFilename As String
        
        Application.DisplayAlerts = False
        Application.EnableEvents = False
        Application.ScreenUpdating = False
        MyPath = Range("pathW").Value ' change to suit
        Set wbDst = Workbooks.Add(xlWBATWorksheet)
        strFilename = Dir(MyPath & "\*.csv", vbNormal) '"\*.xls"
        
        If Len(strFilename) = 0 Then Exit Sub
        
        Do Until strFilename = ""
            
                Set wbSrc = Workbooks.Open(FileName:=MyPath & "\" & strFilename)
                
                Set wsSrc = wbSrc.Worksheets(1)
                
                wsSrc.Copy After:=wbDst.Worksheets(wbDst.Worksheets.Count)
                
                wbSrc.Close False
            
            strFilename = Dir()
            
        Loop
        wbDst.Worksheets(1).Delete
        
        For i = 1 To Sheets.Count
        
        ActiveWorkbook.Sheets(i).Select
        Range("A1").Select
        Range(Selection, Selection.End(xlDown)).Select
        Selection.TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _
            TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
            Semicolon:=True, Comma:=False, Space:=False, Other:=False
        
        Next i
        
        IgualNumeroFilas
        
        ActiveWorkbook.Close saveChanges:=True, FileName:=MyPath & "/Master.xlsx"
            
        Application.DisplayAlerts = True
        Application.EnableEvents = True
        Application.ScreenUpdating = True
        
    End Sub

    It has been working fine during some time until I faced the workbook attched. If I double-click on the file then you may see that the file it's ok and all columns and rows are not equal to zero. However, when running the macro something is going wrong and the master file that is created has a lot of blanks (missing information) on it. Does anybody know what's wrong and how can I solve it?

    I've been trying hard to make it work but finally I always get the same result, only the first 25 columns are filled up instead of 380.
    Attached Files Attached Files
    Last edited by dvb_24; 12-07-2012 at 07:33 PM.

+ 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