Consulting

Results 1 to 7 of 7

Thread: Problems with copying multiple worksheets into one workbook

  1. #1

    Problems with copying multiple worksheets into one workbook

    I'm trying to copy 70 different Excel-files into one workbook. I have script which are able to put togheter upto 11 different files into one workbook. But after this it starts to copy the same name over again. Can anyone help me to modify my script? I'm a Newbeginner in this.

    Here is my script:


    Sub Merge2MultiSheets()
    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 = "H:\My Documents\Test"
        Set wbDst = Workbooks.Add(xlWBATWorksheet)
        strFilename = Dir(MyPath & "\*.xls", vbNormal)
        
        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
        
        End Sub
    It seems to me that this line is the problem

     wsSrc.Copy After:=wbDst.Worksheets(wbDst.Worksheets.Count)
    Since all my worksheets have the same name, when it put's them into the New workbook, it gives it a number at the end. When it come to 10, it stops. How do I make it add more worksheets?

  2. #2
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    welcome to vbax.

    tested with 200 files with success

    credits: snb / kh

    Sub vbax_54318_Consolidate_WorkSheets_From_files_In_Same_Folder()
    
        Dim FolderPath As String
        Dim FilesInFolder
        Dim j As Long, calc As Long
        Dim wbDst As Workbook
    
        With Application
            .ScreenUpdating = False
            .DisplayAlerts = False
            .EnableEvents = False
            .AskToUpdateLinks = False
            calc = .Calculation
            .Calculation = xlCalculationManual
        End With
    
        FolderPath = "H:\My Documents\Test\"
        FilesInFolder = Split(CreateObject("WScript.Shell").Exec("cmd /c Dir """ & FolderPath & "*.xl??"" /b").StdOut.ReadAll, vbCrLf)
        
        Set wbDst = Workbooks.Add(xlWBATWorksheet)
        
        For j = LBound(FilesInFolder) To UBound(FilesInFolder)
            If Len(FilesInFolder(j)) > 4 Then
                With GetObject(FolderPath & FilesInFolder(j))
                        .Worksheets(1).Copy After:=wbDst.Worksheets(wbDst.Worksheets.Count)
                        ActiveSheet.Name = "wb_" & j 'change copied sheet name to workbook's index number in array. you may wish to change this naming structure.
                        .Close 0
                End With
            End If
        Next
    
        wbDst.Worksheets(1).Delete
    
        With Application
            .EnableEvents = True
            .AskToUpdateLinks = True
            .Calculation = calc
        End With
    
    End Sub
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  3. #3
    Thanks! I able to run the script now. I had another problem as well. My filename contained special characters ( ) in Norwegian which it didn't like when it read the files. But now it works!

  4. #4
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    welcome.

    then vba Dir function as in your first post is our friend...

    Sub vbax_54318_Consolidate_WorkSheets_From_files_In_Same_Folder_v2()
    
        Dim FolderPath As String, FilesInFolder As String
        Dim j As Long, calc As Long
        Dim wbDst As Workbook
    
        With Application
            .ScreenUpdating = False
            .DisplayAlerts = False
            .EnableEvents = False
            .AskToUpdateLinks = False
            calc = .Calculation
            .Calculation = xlCalculationManual
        End With
    
        FolderPath = "H:\My Documents\Test\"
        
        Set wbDst = Workbooks.Add(xlWBATWorksheet)
        
        FilesInFolder = Dir(FolderPath & "*.xl??")
    
        Do While FilesInFolder <> ""
            Set wb = Workbooks.Open(FolderPath & FilesInFolder)
            With ActiveWorkbook
                .Worksheets(1).Copy After:=wbDst.Worksheets(wbDst.Worksheets.Count)
                ActiveSheet.Name = "wb_" & j
                .Close 0
            End With
            j = j + 1
            FilesInFolder = Dir()
        Loop
        
        wbDst.Worksheets(1).Delete
    
        With Application
            .EnableEvents = True
            .AskToUpdateLinks = True
            .Calculation = calc
        End With
    
    End Sub
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  5. #5
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    you can use v1 via a UDF

    Sub vbax_54318_Consolidate_WorkSheets_From_files_In_Same_Folder_v1()
    
        Dim FolderPath As String, tempStr As String
        Dim FilesInFolder
        Dim j As Long, calc As Long
        Dim wbDst As Workbook
    
        With Application
            .ScreenUpdating = False
            .DisplayAlerts = False
            .EnableEvents = False
            .AskToUpdateLinks = False
            calc = .Calculation
            .Calculation = xlCalculationManual
        End With
    
        FolderPath = "H:\My Documents\Test\"
        FilesInFolder = Split(CreateObject("WScript.Shell").Exec("cmd /c Dir """ & FolderPath & "*.xl??"" /b").StdOut.ReadAll, vbCrLf)
        
        Set wbDst = Workbooks.Add(xlWBATWorksheet)
        
        For j = LBound(FilesInFolder) To UBound(FilesInFolder)
            If Len(FilesInFolder(j)) > 4 Then
                tempStr = FilesInFolder(j)
                tempStr = StripAccentNorsk(tempStr)
                FilesInFolder(j) = tempStr
                With GetObject(FolderPath & FilesInFolder(j))
                        .Worksheets(1).Copy After:=wbDst.Worksheets(wbDst.Worksheets.Count)
                        ActiveSheet.Name = "wb_" & j
                        .Close 0
                End With
            End If
        Next
    
        wbDst.Worksheets(1).Delete
    
        With Application
            .EnableEvents = True
            .AskToUpdateLinks = True
            .Calculation = calc
        End With
    
    End Sub
     
    
    
    
    Function StripAccentNorsk(thestring As String)
    'http://www.extendoffice.com/documents/excel/707-excel-replace-accented-characters.html
    
        Dim A As String, B As String
        Dim i As Integer
        Const AccChars = ""
        Const RegChars = ""
        
        For i = 1 To Len(AccChars)
            A = Mid(AccChars, i, 1)
            B = Mid(RegChars, i, 1)
            thestring = Replace(thestring, A, B)
        Next
        
        StripAccentNorsk = thestring
    
    End Function
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  6. #6
    Thanks a lot Mangubus. That solved the problem With my Norwegian characters.

  7. #7
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    you are welcome.

    mark the thread as solved from thread tools pls.
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

Posting Permissions

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