+ Reply to Thread
Results 1 to 6 of 6

Read first and second column from different workbooks and save this in one sheet.

  1. #1
    Forum Contributor
    Join Date
    02-05-2021
    Location
    Netherlands
    MS-Off Ver
    Microsoft Excel 365 (version 2022, Build 14931.20764)
    Posts
    180

    Read first and second column from different workbooks and save this in one sheet.

    Dear Excel-experts,

    I have a VBA question.

    Context:
    - I have multiple Excel files with exactly the same layout.
    - All Excel files are saved in one folder (C:\Temp\SourceFolder\*.xlm).
    - Each Excel-file has 12 sheets (tabs) with month names included, such as: (2022 (1) - 2022 (2) - 2022 (3)2022 (4)2022 (5), and so on. Until the 12th month, December (2022 (12)….

    What I like to achieve/have is:
    - An input box with the question which sheet (tab) I like to read from all files in the source folder. For example: Which sheet you want to read? Input is (2022 (1) or 2022 (2) or 2022 (3) or a different month;
    - Read the first and second columns from all Excel source-files that are placed in that specific folder (C:\Temp\SourceFolder\*.xlm).

    - Make a copy of both rows (first en second one). This means per Excel-file from the source folder (C:\Temp\SourceFolder\*.xlm) and copy this information into one Excel-destination file (C:\Temp\Excel_Total_Result.xlm or something).
    - Put all columns (first en second one) next to the other in the destination file (when finished, save the result).

    Result: 1 destination file with all first and second columns read (from all files from the source folder), that are put next to each other in one destination file/sheet (like a summary).
    Is this easy to create?

    Thank you very much for any help provided. :-)


    Best regards,

    Hans

  2. #2
    Valued Forum Contributor
    Join Date
    11-04-2018
    Location
    Denpasar
    MS-Off Ver
    Excel 2010
    Posts
    777

    Re: Read first and second column from different workbooks and save this in one sheet.

    If I'm not mistaken to get what you mean, maybe something like this ?
    Please Login or Register  to view this content.
    Make a new workbook where you want the result is.
    Put the macro above in this new workbook.
    The macro assumed that all the files are in one folder, Drive D - folder test.
    The result will be put in the this new workbook Sheet1.

    What the macro do :
    1. Ask the user to type the name of the sheet he wants the result to be put in Sheet1
    2. Open each file in D:/test
    3. Copy column A and B of the sheet name (inputted by the user) from the opened file
    4. Paste to column A and B of Sheet1 on the first itteration
    5. Close the file
    6. Open the next file (point 2) and do point 3
    7. Paste to column C and D of Sheet1 on the 2nd itteration
    and so on until all the files in D:/test has been opened and copied.

  3. #3
    Forum Contributor
    Join Date
    02-05-2021
    Location
    Netherlands
    MS-Off Ver
    Microsoft Excel 365 (version 2022, Build 14931.20764)
    Posts
    180

    Re: Read first and second column from different workbooks and save this in one sheet.

    Hi Karmapala,

    Thank you for your support. :-)
    I think this is almost what I am looking for.

    I created a test:
    I put two Excel test files in D:\Test --> 1.xls and 2.xls
    - 1.xls : contains sheetname (Sheet1). Two columns with data (Column 1 has value: Pete, Colum 2 has value: John)
    - 2.xls : contains sheetname (Sheet1). Two columns with data (Column 1 has value: Margret, Colum 2 has value: Frank)

    I like to have all de data from 1.xls and 2.xls (both columns) in 1 file, for example:
    - Result.xls: Sheetname (Sheet1), Values: Column 1: Peter, Column 2: John, Column 3: Margret, Column 4: Frank)

    Depending of the amount of files in D:\Test, the results will expand (Column 5:x, Column 6:y) and so on

    Is this easy to create?

  4. #4
    Valued Forum Contributor
    Join Date
    11-04-2018
    Location
    Denpasar
    MS-Off Ver
    Excel 2010
    Posts
    777

    Re: Read first and second column from different workbooks and save this in one sheet.

    Hi NewBee_HS....
    I'm sorry I don't understand what you mean.

    The code in my post is just copying whatever value in Column 1 (column A) and Column 2 (column B) of the opened workbook (in your case 1.xls and 2.xls) of the sheet name you input, to the active sheet where you run the code.

    So for testing, create a new workbook, save and name the wb : Result.xlsm (just don't save it in D:\test).
    Put the code in Result.xlsm module. Within any blank sheet, run the macro.
    If in 1.xls Sheet1 (because you type "Sheet1" in input box), column A has a value Peter in nth row, and a value Parker in another nth row, and column B Sheet1 has a value John in nth row and a value Travolta in another nth row, those value will show in Result.xlsm in column A (Peter in n row, Parker in another n row), column B (John in n row, Travolta in another n row.). The same thing with 2.xls, but this time : Margret is in column C and Frank is in column D.

    But one thing I just realized now, I think you want : when the macro is run again and again it's not overwriting the result before.
    For example, if the next time you run the macro and input "Sheet2", assumed that there are 3 files in D:\test, then the next result will be put in Column 7 to 12. (Because the first time you run the macro, your input "Sheet1" give a result from column 1 to 6.

    Condition in D:/test there are 3 files
    1.xls
    sheet1 column A has 1 value, column B has 2 value
    sheet2 column A has 3 value, column B has 4 value
    sheet3 column A has 5 value, column B has 6 value

    2.xls
    sheet1 column A has 7 value, column B has 8 value
    sheet2 column A has 9 value, column B has 10 value
    sheet3 column A has 11 value, column B has 12 value

    3.xls
    sheet1 column A has 13 value, column B has 14 value
    sheet2 column A has 15 value, column B has 16 value
    sheet3 column A has 17 value, column B has 18 value

    The first time you run the macro, you input "Sheet3".
    The result in Result.xlsm (whatever active blank sheet) :
    column 1 to 6 has value 5, 6, 11, 12, 17, 18 respectively

    The second time you run the macro, you input "Sheet1"
    The result in Result.xlsm (where the active sheet column 1 to 6 already has the first result) :
    column 7 to 12 has value 1, 2, 7, 8, 13, 14 repectively.

    Is that what you mean ?

    OR.... maybe :
    when you run the macro, it will always create a new wb.
    In this case, the scenario is something like this :
    1. D:\test has 3 files
    2. a workbook "macro.xlsm" where the code is
    3. D:\tmp is empty.

    When you run the code within macro.xlsm, you input "Sheet1".
    The result is in D:\tmp with a workbook name (for example) : "Sheet1.xls".
    If you open Sheet1.xls, you see 1, 2, 7, 8, 13, 14 in column 1 to 6 respectively.

    Next time, you run the macro again within macro.xlsm, this time you input "Sheet2"
    The result is in D:\tmp with a workbook name (for example) : "Sheet2.xls".
    When you open Sheet2.xls, you see 3, 4, 9, 10, 15, 16 in column 1 to 6 respectively.

    Maybe like that ?
    Sorry, if I can't get what you mean.
    Last edited by karmapala; 02-15-2022 at 11:54 PM.

  5. #5
    Forum Contributor
    Join Date
    02-05-2021
    Location
    Netherlands
    MS-Off Ver
    Microsoft Excel 365 (version 2022, Build 14931.20764)
    Posts
    180

    Re: Read first and second column from different workbooks and save this in one sheet.

    Hi Karmapala,

    I think that something went wrong with the language settings in my Excel version (English - > Dutch). This was my mistake.
    Everything works like a charm now. So I like to thank you for your time (explanation) and effort to help me out!!!

    Great support. Thank you again.

    Best regards,

    Hans

  6. #6
    Valued Forum Contributor
    Join Date
    11-04-2018
    Location
    Denpasar
    MS-Off Ver
    Excel 2010
    Posts
    777

    Re: Read first and second column from different workbooks and save this in one sheet.

    HI NewBee_HS,
    Glad I can help, and thank you for the rep

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 5
    Last Post: 07-19-2020, 03:49 PM
  2. [SOLVED] Read sheets from different workbooks and copy all content from a certain sheet
    By excelactuary in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 06-13-2019, 11:32 AM
  3. Help to use Sheet name to read other workbooks
    By vidaLL in forum Excel General
    Replies: 2
    Last Post: 02-21-2018, 06:38 AM
  4. Help to use Sheet name to read other workbooks
    By vidaLL in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-20-2018, 03:09 PM
  5. [SOLVED] Save Multiple Worksheets as New Workbooks Based on Sheet Prefixes
    By hamidxa in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 01-09-2015, 02:56 PM
  6. save Active tab to new workbooks based on value in column
    By Zyphon in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 03-04-2010, 12:49 PM
  7. Disable Save As in Read Only workbooks
    By suss-hmfc in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-17-2008, 03:11 PM

Tags for this Thread

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