+ Reply to Thread
Results 1 to 7 of 7

Import 2 most recent files as sheets in existing workbook (VBA)

  1. #1
    Registered User
    Join Date
    01-09-2021
    Location
    Eureka, CA
    MS-Off Ver
    365
    Posts
    5

    Import 2 most recent files as sheets in existing workbook (VBA)

    Hello,
    I have a program that downloads 2 randomly named .csv files into my downloads folder. I am looking to add these files as new sheets in an existing workbook. The code below gets me very close by opening the 2 most recently downloaded .csv files as new workbooks, so I just need them to be added as new worksheets instead. I cannot reference the sheets directly due to the random naming.

    Please Login or Register  to view this content.
    Last edited by cjohns; 01-09-2021 at 05:57 PM.

  2. #2
    Banned User!
    Join Date
    02-06-2020
    Location
    Iowa City, IA, USA
    MS-Off Ver
    2016 - 365 / 2007
    Posts
    2,014

    Re: Import 2 most recent files as sheets in existing workbook (VBA)

    you said:
    I cannot reference the sheets directly due to the random naming.
    yes you can. more than likely, wherever you get them from has caused them to have some sort of consistency. thus, if the sheets you want are always on, say, the left side of the sheets that are at the bottom of the book when you open them (which *should* be the case, because CSV files shouldn't have more than one sheet in them anyway. they are not XL books, technically), just use the index. it's floor is ONE:
    Please Login or Register  to view this content.
    so, open them, copy the sheets, paste them into your target book, and you're done. simple. to get the copy code, do it manually with the macro recorder and see what you get. that's how you learn from the wheel that was already invented by microsoft decades ago.

  3. #3
    Registered User
    Join Date
    01-09-2021
    Location
    Eureka, CA
    MS-Off Ver
    365
    Posts
    5

    Re: Import 2 most recent files as sheets in existing workbook (VBA)

    I have tired this and it does not seem to work. To clarify, the .csv file name is randomly generated by its source, so there is not a consistent book_name that I can use. When I record the macros it records the current bookname rather than referencing it by its file location (i.e., the most recent and 2nd most recent .csv files in my downloads folder).
    Last edited by cjohns; 01-09-2021 at 03:48 PM.

  4. #4
    Banned User!
    Join Date
    02-06-2020
    Location
    Iowa City, IA, USA
    MS-Off Ver
    2016 - 365 / 2007
    Posts
    2,014

    Re: Import 2 most recent files as sheets in existing workbook (VBA)

    OK, so can you upload the book that is in error? include all the code in it, and comment where the problem code is at. your description is not clear to me, still. the randomness of generation of *anything* is completely irrelevant in this case.

  5. #5
    Registered User
    Join Date
    01-09-2021
    Location
    Eureka, CA
    MS-Off Ver
    365
    Posts
    5

    Re: Import 2 most recent files as sheets in existing workbook (VBA)

    Thanks for your time. I have uploaded the workbook with comments in Module1
    Attached Files Attached Files
    Last edited by cjohns; 01-09-2021 at 09:48 PM.

  6. #6
    Registered User
    Join Date
    01-09-2021
    Location
    Eureka, CA
    MS-Off Ver
    365
    Posts
    5

    Re: Import 2 most recent files as sheets in existing workbook (VBA)

    Never mind, I fixed it, I had misunderstood your explanation but was able to acheive the desired result by adding these lines at the end of my code:
    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    01-09-2021
    Location
    Eureka, CA
    MS-Off Ver
    365
    Posts
    5

    Re: Import 2 most recent files as sheets in existing workbook (VBA)

    Doh, back to the drawing board. The code worked perfectly at first, but when I brought new files into the download folder it will open wb1 but gives error 1004 when trying to open wb2, saying it cannot find my download folder and it may have been moved/deleted...
    I reuploaded the workbook with the code that was working before I added new files to the target folder (downloads).
    Please Login or Register  to view this content.
    Last edited by cjohns; 01-09-2021 at 09:49 PM.

+ 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] Import delimited text files as new sheets in current workbook.
    By spencer_time in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-10-2019, 08:08 AM
  2. Replies: 3
    Last Post: 07-06-2016, 07:47 PM
  3. Import Multiple Text Files into Single Workbook with Different Sheets
    By oliver.marshall001 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-21-2014, 09:29 AM
  4. [SOLVED] Import multiple csv files into current workbook as separate sheets
    By santosh in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 05-31-2013, 04:19 AM
  5. Replies: 4
    Last Post: 05-17-2013, 05:29 AM
  6. [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
  7. Replies: 2
    Last Post: 08-30-2007, 12:34 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