• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

import multiple excel files into one with multiselect function

Marco1975

New Member
Hello from the time I'm using a macro that choosing the directory I care all the files into one Excel file . But now I have to import specific files and not all those in the folder as I did before . I did some research and saw that I can choose the files with " multiselect " but I can not understand how the macro should be modified. This is the macro I use now :

Code:
Sub COPIA_Files()


    Set objFSO = CreateObject("Scripting.FileSystemObject")
    InitialFoldr$ = "C:\" '<<< Startup folder
    With Application.FileDialog(msoFileDialogFolderPicker) 'User input for folder to look at
        .InitialFileName = Application.DefaultFilePath & "\"
        .Title = "Please select a folder to list Files from"
        .InitialFileName = InitialFoldr$
        .Show
        If .SelectedItems.Count = 0 Then Exit Sub
        mfolder = .SelectedItems(1) & "\"
    End With
   

    Dim nomeFile As String
    Dim wb As Workbook
    Dim ws As Worksheet
 
   
   
    nomeFile = Dir(mfolder)
    Do While nomeFile <> ""
        If nomeFile <> ThisWorkbook.Name Then
            Set wb = Application.Workbooks.Open(mfolder & nomeFile)
            Set ws = wb.Worksheets(1)
            ws.Name = Left(nomeFile, Len(nomeFile) - 4)
            ws.Copy After:=ThisWorkbook.Worksheets(ThisWorkbook.Worksheets.Count)
            wb.Close False
        End If
        nomeFile = Dir
    Loop
   
    msgbox "Files Importati", vbInformation
    main

   
End Sub

Sorry for my english.
I hope someone can help me . Thank you.
 
Hi Marco
I'm guessing 'main' refers to a different macro so I left it in.
Try this:
Code:
Sub COPIA_Files()
    Dim wb As Workbook
    Dim ws As Worksheet
    Dim fd As FileDialog
    Set fd = Application.FileDialog(msoFileDialogFilePicker)
    With fd
        If .Show = -1 Then
            For Each wb In .SelectedItems
                Set ws = wb.Worksheets(1)
                ws.Name = Left(wb.Name, Len(wb.Name) - 4)
                ws.Copy After:=ThisWorkbook.Worksheets(ThisWorkbook.Worksheets.Count)
                wb.Close False
            Next wb
        End If
    End With
    MsgBox "Files Importati", vbInformation
    main
End Sub

The code should pop open a window with which you can navigate to the right folder and select the files you want to use.
 
Back
Top