+ Reply to Thread
Results 1 to 4 of 4

Import and update several CSV files automatically into Excel workbook

  1. #1
    Registered User
    Join Date
    05-22-2015
    Location
    Copenhagen
    MS-Off Ver
    2010
    Posts
    4

    Import and update several CSV files automatically into Excel workbook

    I have a folder containing multiple CSV files and I have another folder containing an Excel workbook. I want to import all the CSV files from the workbook by the click of a button into seperate sheets (fx 3.csv will have sheetname "3" etc.) without having to specify which one to open. These CSV files are updated frequently, however not always on the same day. I want the import code to also update/overwrite whatever CSV files have been updated. I assume that this is implicitly done every time I import all the files.

    The following code does the trick..well almost. The problem is whenever I click on the button it doesn't overwrite the existing sheets. It adds new sheets. Let's say I upload (for the very first time). The sheets inside the workbook are called city1.csv, city2.csv etc.. The second time I run the code, it has added another range of sheets city1, city2 etc. The third time city1(1), city2(2) etc.

    How can I make the import code to overwrite every time I click on the button instead of adding new sheets once the first sheets have been added?

    Please Login or Register  to view this content.
    I assume the problem is in the "mybook.Worksheets(1).Copy after:="-part. Don't know how to let the workbook recognize existing sheets (while also being able to import the CSV files the first time)

    Thx!
    Last edited by JBeaucaire; 07-31-2015 at 11:15 AM. Reason: Added missing CODE tags. Please read and follow the Forum Rules, link above in the menu bar. Thanks.

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Import and update several CSV files automatically into Excel workbook

    I think like so:

    Please Login or Register  to view this content.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Registered User
    Join Date
    05-22-2015
    Location
    Copenhagen
    MS-Off Ver
    2010
    Posts
    4

    Re: Import and update several CSV files automatically into Excel workbook

    Hi JBeaucaire

    Just tried your code with the added cleanup. Still doesn't work. The first time I click on the buttons the three CSV files have been succesfully imported with the respective sheetnames (city1.csv, city2.csv etc.) and second time I click on the button, it adds new sheets and doesn't overwrite.

  4. #4
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Import and update several CSV files automatically into Excel workbook

    I just range my code on the same folder twice, and both times it brought in the new sheet and removed the old if it was there already.

+ 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] Update automatically name range once file is import
    By emina002 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-30-2015, 03:31 PM
  2. [SOLVED] Macro to Import Multiple TXT Files into workbook - User to select files/directory
    By saber007 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 05-15-2013, 08:43 PM
  3. Help to import multiple text files to worksheets in a excel workbook
    By cookiemonster79 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 10-31-2011, 02:17 PM
  4. Replies: 5
    Last Post: 07-09-2011, 08:30 AM
  5. Replies: 3
    Last Post: 02-11-2011, 11:26 PM
  6. How to automatically update workbook linked to other excel files?
    By newnothing in forum Excel - New Users/Basics
    Replies: 7
    Last Post: 07-25-2009, 11:57 AM

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