+ Reply to Thread
Results 1 to 8 of 8

Save workbook as two separate formats (CSV and XLSX) and leave XLSX open

  1. #1
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Save workbook as two separate formats (CSV and XLSX) and leave XLSX open

    I have a macro that creates a report in a new unsaved workbook.
    1. I want the workbook to be saved as XLSX format - and remain open after saving.
    2. While saving the XLSX, I also want a copy of this workbook to be saved in CSV format (without any warnings about possible data loss or incompatibility with CSV format)(I accept the risk for this particular report)
    3. After the macro has finished running, I should end up with a CSV and a XLSX saved in the same location (Documents path) and only the XLSX file should be open.
    4. The macro should run with no prompts (the location and filenames will be preset by the macro)

    I think I can handle writing all of this except for the CSV aspect. Help would be appreciated.

    (I want two formats because XLSX is more readable - with tables and filters - but I need a copy in CSV for importing to other programs)



    UPDATE:
    My post wasn't as clear as I thought it was. Clarifying the above requirements:
    • The macro exists outside of the workbook that is generated. "I have a macro that creates a report in a new unsaved workbook".
    • The XLSX needs to retain all of its formatting. The CSV will have to be created from a copy of the open workbook. "XLSX is more readable - with tables and filters - but I need a copy in CSV for importing" & "(without any warnings about possible data loss or incompatibility with CSV format)(I accept the risk for this particular report)"
    Last edited by mc84excel; 12-01-2019 at 09:56 PM.
    *******************************************************

    HELP WANTED! (Links to Forum threads)
    Trying to create reusable code for Custom Events at Workbook (not Application) level

    *******************************************************

  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,874

    Re: Save workbook as two separate formats (CSV and XLSX) and leave XLSX open

    Maybe something like this. I saved the excel file as a macro enabled file as you need to store the macro somewhere.

    Please Login or Register  to view this content.
    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
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Save workbook as two separate formats (CSV and XLSX) and leave XLSX open

    Thanks for the suggestion Alan.

    Quote Originally Posted by alansidman View Post
    I saved the excel file as a macro enabled file as you need to store the macro somewhere
    The macro and the output report are in separate files. (As my opening post "macro that creates a report in a new unsaved workbook")

    When I run the macro, it produces a report in a new workbook. It's this new unsaved workbook that I want to save as a XLSX and CSV.

    Also you are using
    Please Login or Register  to view this content.
    to save as CSV before saving as XLSX. Saving a workbook as a CSV will cause it to lose anything that isn't compatible with this format - such as formatting and tables. As noted in the opening post, I want a copy of the workbook to be saved as a CSV. I don't want to lose formatting from the XLSX file.

  4. #4
    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,874

    Re: Save workbook as two separate formats (CSV and XLSX) and leave XLSX open

    How about just using the Macro Recorder and then post here for changes that are required to make it more dynamic.

  5. #5
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Save workbook as two separate formats (CSV and XLSX) and leave XLSX open

    I think I made my original question too specific.

    I'll reframe the question: Suppose I have VBA referencing a XLSX file that is currently open (Assume that the XLSX has already been saved). I want:
    1. a copy of the open XLSX saved as a CSV file (It doesn't matter if the CSV file will lose formatting or anything incompatible with the CSV format)
    2. without any prompt for where the CSV will be saved (The CSV should be in the same folder as the XLSX. This will be application.defaultuserpath if that helps. Filename will be preset. Call it Import.csv)
    3. without any warning message re feature loss in the CSV
    4. without needing to close the XLSX at any point
    5. without losing any data or formatting from the open XLSX

  6. #6
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Save workbook as two separate formats (CSV and XLSX) and leave XLSX open

    Copy the workbook
    Save the copy as CSV
    Close the copy
    Save the original as xlsx

    Please Login or Register  to view this content.
    Last edited by AlphaFrog; 12-02-2019 at 11:39 PM.
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  7. #7
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Save workbook as two separate formats (CSV and XLSX) and leave XLSX open

    Thanks AlphaFrog. I will test this code in the next few days and let you know how it goes (and if it solves it, will rep you ofc)

    (BTW there is a minor error but I can handle it. The last line is saving XLSM format not XLSX)

  8. #8
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Save workbook as two separate formats (CSV and XLSX) and leave XLSX open

    Sorry AlphaFrog but I couldn't get your code to work. I get a Runtime error 438 on the line
    Please Login or Register  to view this content.
    I have since solved all of the outcomes in the opening post so I am closing the ticket. (I would have preferred to avoid close & reopen the XLSX but it doesn't seem to be possible to avoid doing that. Unless somebody can offer a different solution?)

    relevant code extract below:

    Please Login or Register  to view this content.
    Last edited by mc84excel; 12-04-2019 at 07:23 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. Save Worksheet to New Workbook .xlsx
    By brentjohn in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 08-03-2019, 02:05 PM
  2. Save Sheet as New Workbook.xlsx with Choices for folder, Name and filetype
    By nordicdust in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-13-2018, 03:50 PM
  3. Split XLSX Workbook into Separate CSV Files
    By freybe06 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-23-2017, 04:36 PM
  4. Replies: 0
    Last Post: 01-14-2016, 03:22 PM
  5. [SOLVED] Save macro workbook as xlsx bypassing msg box
    By kosherboy in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 01-01-2014, 01:21 PM
  6. Replies: 6
    Last Post: 08-28-2012, 03:51 AM
  7. VBA to save a copy of .xlsm workbook as .xlsx
    By lanziniad in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-20-2012, 02:00 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