Consulting

Results 1 to 5 of 5

Thread: Pick directory and merge 2 files into 1 workbook

  1. #1
    VBAX Regular
    Joined
    May 2016
    Posts
    69
    Location

    Pick directory and merge 2 files into 1 workbook

    Hi everyone,

    I am looking for a VBA code that would allow me to pick a directory

    (The files have the same name every month, but the folder changes every month)

    then it would find the 2 files that have a specific string of text in the title (so wildcards are involved).

    So for example, somewhere in the filename, it would have to have the following:

    file 1: Cats-Are-Cute
    file 2: Dogs-Are-Too

    and merge those sheets into a new workbooks named analysis.

  2. #2
    VBAX Regular
    Joined
    May 2016
    Posts
    69
    Location
    OK, I think I got the first part, the option to pick the Directory.

    Sub GetFolder()
    Dim FolderName As String
     With Application.FileDialog(msoFileDialogFolderPicker)
       .AllowMultiSelect = False
       .Show
       On Error Resume Next
       FolderName = .SelectedItems(1)
       Err.Clear
       On Error GoTo 0
     End With
    End Sub

  3. #3
    VBAX Regular
    Joined
    May 2016
    Posts
    69
    Location
    bump...

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

    Sub vbax_61347_select_folder_open_2wbs_merge_into_1wb()
    
        Dim fPath As String
        Dim wb1 As Workbook, wb2 As Workbook
    
        With Application.FileDialog(msoFileDialogFolderPicker)
            If .Show = -1 Then
                fPath = .SelectedItems(1) & "\"
            Else
                Exit Sub
            End If
        End With
    
        Set wb1 = Workbooks.Open(fPath & "*Cats-Are-Cute*.xls?")
        Set wb2 = Workbooks.Open(fPath & "*Dogs-Are-Too*.xls?")
    
        wb1.Worksheets("cat").Copy
        wb2.Worksheets("dog").Copy After:=ActiveWorkbook.Worksheets(1)
        'change cat and dog to suit
    
        ActiveWorkbook.SaveAs fPath & "analysis.xlsx", 51 'save as new file in the same folder
        '51 = xlsx, 52 = xlsm, 50 = xlsb, 56 = xls
    
        wb1.Close False
        wb2.Close False
        'close previously opened files without saving them
    
    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 Regular
    Joined
    May 2016
    Posts
    69
    Location
    Thank you so much, Mancubus!

    I will give it a try.

Tags for this Thread

Posting Permissions

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