Consulting

Results 1 to 5 of 5

Thread: Input 2 files and create 3rd file

  1. #1
    VBAX Regular
    Joined
    Nov 2015
    Posts
    43
    Location

    Input 2 files and create 3rd file

    I have a challenges where I have two input files


    1. Emp-Input.xlsx 2. Zip-Input file.xlsx


    I am creating a 3rd workbook (Emp-Zip-Output.xlsx) file taking data from the two input files.The original file contains thousands of records. Can you suggest a VBA code to automate the process?

    Zip-Emp-OUTPUT File

    ZIP (Column A) : These values comes from “zip-input” file.
    TERRITORY (Column B) : These values comes from “zip-input” file.

    FIRST NAME (Column C) : These values comes from “emp-input” file that match Territory (Column B) against TERRITORY (Column B) of “Output” file.

    LAST NAME (Column D) : These values comes from “emp-input” file that match Territory (Column B) against TERRITORY (Column B) of “Output” file.

    Region(Column E) : These values comes from “emp-input” file that match Territory (Column B) against TERRITORY (Column B) of “Output” file
    Attached Files Attached Files

  2. #2
    VBAX Regular
    Joined
    Nov 2015
    Posts
    43
    Location
    I appreciate any response on this issue.

  3. #3
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    save your output file as xlsm.

    open vbe, paste the below code in a standard module and set a reference to "Microsoft ActiveX Data Objects X.X Library"

    Sub vbax_55490_join_sheets_in_diff_wbs()
    
        Dim cn As New ADODB.Connection, rs As New ADODB.Recordset
        Dim strSQL As String
        Dim j As Long
        
        With Application
            .DisplayAlerts = False
            .ScreenUpdating = False
        End With
        
        cn.Open ("Provider=Microsoft.ACE.OLEDB.12.0;" _
                & "Data Source=" & ThisWorkbook.Path & "\Zip-Input.xlsx;" _
                & "Extended Properties=""Excel 12.0;HDR=Yes;IMEX=1""")
        strSQL = "SELECT * FROM [Sheet1$]"
        rs.Open strSQL, cn, adOpenStatic, adLockReadOnly
        Worksheets.Add.Name = "zip_inp"
        With Worksheets("zip_inp")
            For j = 1 To rs.Fields.Count
                .Cells(1, j).Value = rs.Fields(j - 1).Name
            Next j
            .Range("A2").CopyFromRecordset rs
        End With
        rs.Close
        cn.Close
    
        cn.Open ("Provider=Microsoft.ACE.OLEDB.12.0;" _
                & "Data Source=" & ThisWorkbook.Path & "\Emp-Input.xlsx;" _
                & "Extended Properties=""Excel 12.0;HDR=Yes;IMEX=1""")
        strSQL = "SELECT * FROM [Sheet1$]"
        rs.Open strSQL, cn, adOpenStatic, adLockReadOnly
        Worksheets.Add.Name = "emp_inp"
        With Worksheets("emp_inp")
            For j = 1 To rs.Fields.Count
                .Cells(1, j).Value = rs.Fields(j - 1).Name
            Next j
            .Range("A2").CopyFromRecordset rs
        End With
        rs.Close
        cn.Close
    
        cn.Open ("Provider=Microsoft.ACE.OLEDB.12.0;" _
                & "Data Source=" & ThisWorkbook.FullName & ";" _
                & "Extended Properties=""Excel 12.0;HDR=Yes;IMEX=1""")
        strSQL = _
            "SELECT z.Zip, z.Territory, e.[FIRST NAME], e.[LAST NAME], z.Region " & _
            "FROM [zip_inp$] AS z " & _
            "LEFT JOIN [emp_inp$] AS e ON z.Territory = e.Territory;"
        rs.Open strSQL, cn, adOpenStatic, adLockReadOnly
        With Worksheets("Output")
            .Cells.Clear
            For i = 0 To rs.Fields.Count - 1
                .Cells(1, i + 1).Value = rs.Fields(i).Name
            Next i
            .Cells(2, 1).CopyFromRecordset rs
        End With
        rs.Close
        cn.Close
    
        Worksheets("zip_inp").Delete
        Worksheets("emp_inp").Delete
    
    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)

  4. #4

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

    i assumed all three workbooks are in the same folder, and sheet names are Sheet1. from the feedback i understand it is so.

    if the files are in different folders, ThisWorkbook.Path bit in the connection strings must be replaced with the actual folders the input workbookworks are in. and the Sheet1$ ($ sign is important here) bits must be replaced with the actual sheet names.
    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
  •