+ Reply to Thread
Results 1 to 18 of 18

Macro to copy 2 columns from .csvs to one sheet in another file

  1. #1
    Registered User
    Join Date
    02-06-2014
    Location
    Germany
    MS-Off Ver
    Excel 2007
    Posts
    13

    Macro to copy 2 columns from .csvs to one sheet in another file

    Hello everyone,

    I have quite an amount of .csv files in one folder which I want to merge. The .csvs contain 2 columns of data which I want to put in one workbook.

    Does someone have an idea on how to automatize this?

    Best and thanks upfront for the support
    Micha

  2. #2
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2405 Win 11 Home 64 Bit
    Posts
    23,893

    Re: Macro to copy 2 columns from .csvs to one sheet in another file

    Do you want all the data in two columns on one sheet in your workbook? or do you wish for each .csv file to be on a separate sheet in the new workbook? Are there any files in the folder that will not be copied to the new file? Is the new file also resident in the folder? Are the columns to be copied all in the same location? ie. Columns A & B?
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  3. #3
    Registered User
    Join Date
    02-06-2014
    Location
    Germany
    MS-Off Ver
    Excel 2007
    Posts
    13

    Re: Macro to copy 2 columns from .csvs to one sheet in another file

    Yes in one sheet. No, there are only the csv I want to be in the sheet. Yes its also resident in the folder. Yes they are all in A and B.

    Best
    Micha

  4. #4
    Registered User
    Join Date
    02-06-2014
    Location
    Germany
    MS-Off Ver
    Excel 2007
    Posts
    13

    Re: Macro to copy 2 columns from .csvs to one sheet in another file

    Also, the names of the .csv are according to a list of search queries which are already in an excel file, if that helps.

    Cheers
    Micha

  5. #5
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2405 Win 11 Home 64 Bit
    Posts
    23,893

    Re: Macro to copy 2 columns from .csvs to one sheet in another file

    This link should help you

    http://www.rondebruin.nl/win/s3/win021.htm

  6. #6
    Registered User
    Join Date
    02-06-2014
    Location
    Germany
    MS-Off Ver
    Excel 2007
    Posts
    13

    Re: Macro to copy 2 columns from .csvs to one sheet in another file

    Hey Alan, thanks for the link. The problem with the copy *.csv all.txt command is that it puts all the data beneath the previous one, so not next to each other. Is there a command to do excactly that?

    Best
    Micha

  7. #7
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2405 Win 11 Home 64 Bit
    Posts
    23,893

    Re: Macro to copy 2 columns from .csvs to one sheet in another file

    I don't understand what you mean.

  8. #8
    Registered User
    Join Date
    02-06-2014
    Location
    Germany
    MS-Off Ver
    Excel 2007
    Posts
    13

    Re: Macro to copy 2 columns from .csvs to one sheet in another file

    Ok sorry, let me elaborate.

    My Data in the .csv looks like this

    A B
    search query xxx
    date value
    xxx yyyy
    xxx yyyy
    xxx yyyy
    xxx yyyy

    When I use the copy *.csv all.txt command the created csv looks like this

    A B
    search query xxx
    date value
    xxx yyyy
    xxx yyyy
    xxx yyyy
    xxx yyyy


    search query zzzz
    date value
    xxx zzzz
    xxx zzzz
    xxx zzzz
    xxx zzzz

    but I want it to be like this

    A B C D
    search query xxx search query zzzz
    date value date value
    xxx yyyy xxx zzzzz
    xxx yyyy xxx zzzzz
    xxx yyyy xxx zzzzz
    xxx yyyy xxx zzzzz

    Hope this makes it clearer.

    Best
    Micha

  9. #9
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2405 Win 11 Home 64 Bit
    Posts
    23,893

    Re: Macro to copy 2 columns from .csvs to one sheet in another file

    I have to look at the code explicitly and see what Ron is doing. I don't think that you can do it with that code. You will need to build a loop to loop through each file and copy and then paste. I don't have time right now to develop that for you, but will look at it in the next day or so as time permits. In the mean time, hopefully, someone else will jump in here. I have done something similar to this with .xls? files but I believe it is slightly different for .csv files. Here is a starting point if you want to try yourself.

    http://windowssecrets.com/forums/sho...o-master-sheet
    Last edited by alansidman; 02-23-2014 at 09:24 PM.

  10. #10
    Registered User
    Join Date
    02-11-2014
    Location
    Montreal
    MS-Off Ver
    Excel 2007
    Posts
    13

    Re: Macro to copy 2 columns from .csvs to one sheet in another file

    Quote Originally Posted by meetcha View Post
    but I want it to be like this

    A B C D
    search query xxx search query zzzz
    date value date value
    xxx yyyy xxx zzzzz
    xxx yyyy xxx zzzzz
    xxx yyyy xxx zzzzz
    xxx yyyy xxx zzzzz
    Here is a way of doing it.

    Cheers

    Docmarti
    Attached Files Attached Files

  11. #11
    Registered User
    Join Date
    02-06-2014
    Location
    Germany
    MS-Off Ver
    Excel 2007
    Posts
    13

    Re: Macro to copy 2 columns from .csvs to one sheet in another file

    Hey Doc,

    thanks a lot, much appreciated! Unfortunately the macro does not seem to work, nothing happens. Do I have to modify something to fit my system? I already tried changing the path according to where the *.csv are located on my computer but that did not help.

    Best
    Micha

  12. #12
    Registered User
    Join Date
    02-11-2014
    Location
    Montreal
    MS-Off Ver
    Excel 2007
    Posts
    13

    Re: Macro to copy 2 columns from .csvs to one sheet in another file

    Quote Originally Posted by meetcha View Post
    Hey Doc,

    thanks a lot, much appreciated! Unfortunately the macro does not seem to work, nothing happens. Do I have to modify something to fit my system? I already tried changing the path according to where the *.csv are located on my computer but that did not help.
    Edit:
    You must modify the following line according to your own directory path

    Please Login or Register  to view this content.
    Docmarti
    Last edited by Docmarti; 02-25-2014 at 08:15 PM.

  13. #13
    Registered User
    Join Date
    02-06-2014
    Location
    Germany
    MS-Off Ver
    Excel 2007
    Posts
    13

    Re: Macro to copy 2 columns from .csvs to one sheet in another file

    Hi Doc,

    already tried, unfortunately nothing happens.

    Best
    Micha

  14. #14
    Registered User
    Join Date
    02-06-2014
    Location
    Germany
    MS-Off Ver
    Excel 2007
    Posts
    13

    Re: Macro to copy 2 columns from .csvs to one sheet in another file

    Hi Doc,

    already tried, unfortunately nothing happens.

    Best
    Micha

  15. #15
    Registered User
    Join Date
    02-11-2014
    Location
    Montreal
    MS-Off Ver
    Excel 2007
    Posts
    13

    Re: Macro to copy 2 columns from .csvs to one sheet in another file

    Hi Micha

    The code for my path is :
    Please Login or Register  to view this content.
    What is yours ?


    Docmarti

  16. #16
    Registered User
    Join Date
    02-06-2014
    Location
    Germany
    MS-Off Ver
    Excel 2007
    Posts
    13

    Re: Macro to copy 2 columns from .csvs to one sheet in another file

    Hi Doc,

    thanks for the ongoing support. Mine is

    PHP Code: 
     ThePath "C:\Users\M\Desktop\New folder\csv us Harvard" 
    Best
    Micha

  17. #17
    Registered User
    Join Date
    02-11-2014
    Location
    Montreal
    MS-Off Ver
    Excel 2007
    Posts
    13

    Re: Macro to copy 2 columns from .csvs to one sheet in another file

    Hi Micha

    There must be a "\" at the end of your path. Sorry.

    ThePath = "C:\Users\M\Desktop\New folder\csv us Harvard\"

    Docmarti
    Last edited by Docmarti; 02-25-2014 at 08:20 PM.

  18. #18
    Registered User
    Join Date
    02-06-2014
    Location
    Germany
    MS-Off Ver
    Excel 2007
    Posts
    13

    Re: Macro to copy 2 columns from .csvs to one sheet in another file

    Uff, sorry my fault! Now it works like a charm! Thank you so much man, you saved me a lot of time! Much appreciated!

    Hows the time travelling going btw?

  19. #19
    Registered User
    Join Date
    02-11-2014
    Location
    Montreal
    MS-Off Ver
    Excel 2007
    Posts
    13

    Re: Macro to copy 2 columns from .csvs to one sheet in another file

    Just fine.
    I knew you were going to like that code.

+ 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. [SOLVED] Copy data from Specific columns from a csv file and pasting it in the Active sheet
    By Pavan Renjal in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 04-01-2014, 12:14 PM
  2. Macro To Copy Columns From One File to Another
    By kikar in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-10-2014, 12:44 PM
  3. Replies: 4
    Last Post: 09-03-2013, 09:56 AM
  4. Macro to copy 3 columns of data from multiple sheets into 3 columns on 1 sheet
    By bballdcm2007 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 12-15-2012, 04:37 PM
  5. Macro for find and replacing values in multiple CSVs
    By kali237 in forum Excel General
    Replies: 4
    Last Post: 10-09-2011, 08:10 AM

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