+ Reply to Thread
Results 1 to 6 of 6

Save the current worrkbook, save the sheet as .csv and close the workbook

  1. #1
    Registered User
    Join Date
    08-10-2015
    Location
    UK
    MS-Off Ver
    2013
    Posts
    2

    Save the current worrkbook, save the sheet as .csv and close the workbook

    Hi All,

    This is my first post and I really appreciate any help I can get!

    I'm trying to save the current sheet into a .csv file before saving & closing the current workbook.

    The code I have at the moment is:

    Const xlCSV = 6

    Set objExcel = CreateObject("Excel.Application")
    objExcel.DisplayAlerts = FALSE
    objExcel.Visible = TRUE

    Set objWorksheet = ActiveSheet
    objWorksheet.SaveAs "c:\Users\Public\test.csv", xlCSV

    ActiveWorkbook.Close TRUE



    This doesn't seem to work at all! Please can you help?

    Tim

  2. #2
    Valued Forum Contributor quekbc's Avatar
    Join Date
    01-18-2010
    Location
    Sydney, Australia
    MS-Off Ver
    2010, 2013, 2016
    Posts
    1,149

    Re: Save the current worrkbook, save the sheet as .csv and close the workbook

    Firstly, I don't see why you need to define xlCSV. It should already be pre-defined so.. scratch that line.

    Secondly, I don't see why you need to create an instance of Excel. You can just use Application.DisplayAlerts / Visible (although I am not sure why you need to change the Visible property)

    I think your code could be as simple as:
    Please Login or Register  to view this content.

  3. #3
    Forum Expert
    Join Date
    11-26-2013
    Location
    Colac, Victoria, Australia
    MS-Off Ver
    Excel 2016
    Posts
    1,309

    Re: Save the current worrkbook, save the sheet as .csv and close the workbook

    Hi Tim, and welcome to the forum.

    This line looks wrong! objWorksheet.SaveAs "c:\Users\Public\test.csv", xlCSV

    I would be inclined to try objWorksheet.SaveAs Filename:= "c:\Users\Public\test.csv", FileFormat:-xlCSV

    Unfortunately, without the whole macro or the workbook, it's difficult to test.

    I hope this helps, please let me know!

    Regards,

    David

    Please remember, your request is clearer if you attach a sample workbook.
    - Click on Go Advanced and click on the Paper Clip.

    If this has been of assistance, please advise.
    - Please click on the *Add Reputation button at the bottom of helpful responses.

    Please mark your thread as SOLVED:
    - Click Thread Tools above your first post, select "Mark your thread as Solved".

    If several people have responded, when you reply please make it clear WHO you are responding
    to by mentioning their name.

  4. #4
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,447

    Re: Save the current worrkbook, save the sheet as .csv and close the workbook

    If you just record a macro while you save the active sheet as a .csv file, you get this:

    Please Login or Register  to view this content.
    and, if you tidy it up a bit, you can get this ... without being prompted by Excel

    Please Login or Register  to view this content.

    Then all you need is to add the lines of code (already provided by quekbc) to save the file and close it.

    Regards, TMS
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  5. #5
    Registered User
    Join Date
    08-10-2015
    Location
    UK
    MS-Off Ver
    2013
    Posts
    2

    Re: Save the current worrkbook, save the sheet as .csv and close the workbook

    Thank you all. I have realised that I am a little more ignorant than I thought I was!
    I am using VBScript in a 3rd party application to control EXCEL. I think that the help you guys have given me is VBA for EXCEL.
    Is this then the wrong forum? If so sorry, if not, further guidance much appreciated!

  6. #6
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,447

    Re: Save the current worrkbook, save the sheet as .csv and close the workbook

    I guess if you are running this from VB rather than VBA in Excel, the problem is that although you have created an instance of Excel, you haven't opened a workbook. So, maybe you need to do that before you try to save it as a .csv file.

    Regards, TMS

+ 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 .xlsx from active sheet, with sheet name, same address of current workbook
    By Mikeswell in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 10-31-2014, 12:14 PM
  2. Macro to Save current XLS, then to save as a CSV. Then close CSV and reopen XLS
    By cricker in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 10-02-2014, 06:17 AM
  3. [SOLVED] How to save sheet data, close workbook, then reopen Original
    By SCHammer843 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-25-2014, 01:11 PM
  4. Replies: 1
    Last Post: 10-25-2013, 06:10 PM
  5. Open MSWord from Excel, merge with current Userform record, save and close Word.
    By jamieswift1977 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-17-2012, 05:28 AM
  6. How to open,save and close another workbook from the current workbook using macro?
    By ravikumar00008 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 06-15-2012, 10:42 AM
  7. Command button - to copy active sheet and not save current workbook
    By vjboaz in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-22-2008, 05:23 PM

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