+ Reply to Thread
Results 1 to 23 of 23

Macro to Save Excel sheet as comma delimited txt file

  1. #1
    Registered User
    Join Date
    04-21-2014
    Location
    US
    MS-Off Ver
    Excel 2010
    Posts
    11

    Macro to Save Excel sheet as comma delimited txt file

    Hi,

    i am new to macros. can anyone help me in providing a macro to save an excel sheet to comma delimited txt file. Also, My sheet has 1st row as table columns and i dont want to export them in my txt file.

    Thanks,
    -Dileep

  2. #2
    Forum Contributor Jack7774's Avatar
    Join Date
    08-31-2012
    Location
    Georgia, US
    MS-Off Ver
    Excel 2010
    Posts
    576

    Re: Macro to Save Excel sheet as comma delimited txt file

    To not include the first row you could always just delete it before saving it.

    Please Login or Register  to view this content.
    Please Login or Register  to view this content.

  3. #3
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Macro to Save Excel sheet as comma delimited txt file

    Hello dileeprajam,

    Welcome to the Forum!

    This macro will save the active sheet of the active workbook as a CSV file.

    You can change the folder the file will be saved to. If you don't then the file will be saved to where the workbook was saved.
    The default file name for the CSV file is the sheet name plus ".csv".

    It is assumed the data does not contain any commas, the column headers are in row one, and column "A" is the first column.

    Please Login or Register  to view this content.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  4. #4
    Registered User
    Join Date
    04-21-2014
    Location
    US
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: Macro to Save Excel sheet as comma delimited txt file

    Thank you jessebranum777, this code was helpful. i have another requirement:
    now i want to export to csv by excluding 1st 4 columns. i should not delete these because i am using them as references in other columns.
    i tried to hide them and then export but the result csv file still have these columns.

    can you help me on this.
    thanks,
    -Dileep

  5. #5
    Forum Contributor Jack7774's Avatar
    Join Date
    08-31-2012
    Location
    Georgia, US
    MS-Off Ver
    Excel 2010
    Posts
    576

    Re: Macro to Save Excel sheet as comma delimited txt file

    CSV doesn't have hide features to the best of my knowledge so you are correct that won't work. Are you saying you need the first four columns on the CSV file too? If so I don't understand why you don't want to export them. If not then why not delete them from the export if they are not needed? I guess if you could upload a excel sheet with what you have and what you expect I could understand better.

  6. #6
    Registered User
    Join Date
    04-21-2014
    Location
    US
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: Macro to Save Excel sheet as comma delimited txt file

    Hi,

    i have attached my Excel workbook.

    In this workbook, I have two sheets "Test", "Output Data". Test sheet has my original data and i am using formulas to pull required data ans saving in "Output Data".

    Now i have to export the "Output Data" sheet to csv file but i dont want Columns(A:E) to be exported
    The reason why i am not deleting those columns is if you look at the sheet "Output Data".K2 cell, i am using formulas referencing those "A:E" columns.

    Also, i dont want to export my header row.

    so my o/p csv file should be like

    Mgr1,1234,4,15,14,24,1,10046,1,0,,0,,,,,,,,,,,,,,,,,
    mgr2,5678,4,15,14,24,1,8403,1,0,,0,,,,,,,,,,,,,,,,,
    Mgr3,91011,4,15,14,24,1,12024,1,0,,0,,,,,,,,,,,,,,,,,


    Appreciate your prompt responses.

    thanks,
    Dileep
    Attached Files Attached Files

  7. #7
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Macro to Save Excel sheet as comma delimited txt file

    Hello dileeprajam,

    I have modified my original macro to exclude the first 5 columns (A:E) as you asked. The macro has been added to the attached workbook.
    Please Login or Register  to view this content.
    Attached Files Attached Files

  8. #8
    Registered User
    Join Date
    04-21-2014
    Location
    US
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: Macro to Save Excel sheet as comma delimited txt file

    Thanks for the code; I am calling this code in Access Button click event and it works fine for the first run. Now when i run this without closing the application i am getting an error message "Method 'Columns' of object '_Global' failed" at the line LastCol = Wks.Cells(Rng.Row, Columns.Count).end(xlToLeft).Column. can you explain why am i getting this error?

    thanks for all the help.

  9. #9
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Macro to Save Excel sheet as comma delimited txt file

    Hello dileeprajam,

    That particular error is caused when there is no data in row 1 on the worksheet.

  10. #10
    Registered User
    Join Date
    04-21-2014
    Location
    US
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: Macro to Save Excel sheet as comma delimited txt file

    but Ross, it works fine for the 1st run and we are not deleting any data from the sheet. So, data should be there in Row1 for the 2nd run right? Am i missing something here?

  11. #11
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Macro to Save Excel sheet as comma delimited txt file

    Hello dileeprajam,

    I agree it should work if no data has been deleted. Have you made any changes to your workbook since you posted it?

  12. #12
    Registered User
    Join Date
    04-21-2014
    Location
    US
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: Macro to Save Excel sheet as comma delimited txt file

    No, i did not do any changes to it. actually in my button click event, i am opening the file similar to what i have shared. it has one i/p sheet ("Test") and multiple o/p sheets "Output1" "Output2" etc... i am looping through all those sheets and used your code to generate several csv files with Output1, Output2 etc.. as names.

    this process works good in the 1st run but fails if i run for second run.
    Below is the button click event:
    Please Login or Register  to view this content.

  13. #13
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Macro to Save Excel sheet as comma delimited txt file

    Hello dileeprajam,

    Try this modified version of your macro. The macro needed some changes because of late binding to Excel.
    Please Login or Register  to view this content.

  14. #14
    Registered User
    Join Date
    04-21-2014
    Location
    US
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: Macro to Save Excel sheet as comma delimited txt file

    No Ross, still facing the same error

  15. #15
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Macro to Save Excel sheet as comma delimited txt file

    Hello dileeprajam,

    Can more than one person access this workbook at the same time? Sharing a workbook can cause problems.

  16. #16
    Registered User
    Join Date
    04-21-2014
    Location
    US
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: Macro to Save Excel sheet as comma delimited txt file

    No, its only one person who would do this and ideally it would be run only once for each file. I am testing this for multiple runs to ensure if the user clicks it twice, it doesnot throw any error.

  17. #17
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Macro to Save Excel sheet as comma delimited txt file

    Hello dileeprajam,

    Since all other changes have had no effect, then the worksheet you are accessing is empty. There is no other explanation that explains the error.

  18. #18
    Registered User
    Join Date
    04-21-2014
    Location
    US
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: Macro to Save Excel sheet as comma delimited txt file

    Ok Ross, is there a way where we can check if that sheet is open and is empty so that we can handle it rather than throwing an error message?

  19. #19
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Macro to Save Excel sheet as comma delimited txt file

    Hello dileeprajam,

    This version will skip the worksheet if it is empty.
    Please Login or Register  to view this content.

  20. #20
    Registered User
    Join Date
    04-21-2014
    Location
    US
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: Macro to Save Excel sheet as comma delimited txt file

    Hey Ross,I have modified the code to get last column as LastCol = Wks.Cells(Rng.Row, Sheet.Columns.Count).End(xlToLeft).Column
    now the error was fixed but i am getting same error at Set Row = Intersect(Row, Row.Offset(0, 5)) inside the for loop

  21. #21
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Macro to Save Excel sheet as comma delimited txt file

    Hello

    Nice catch. I missed that on the last update. This For Next loop should work correctly now.
    Please Login or Register  to view this content.

  22. #22
    Registered User
    Join Date
    04-21-2014
    Location
    US
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: Macro to Save Excel sheet as comma delimited txt file

    Hurray, seems like everything looks good now. i tried several runs and it is working fine... Thanks Ross for all the help

  23. #23
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Macro to Save Excel sheet as comma delimited txt file

    Hello dileeprajam,

    Your discovery is what really solved it. Glad I could help get it working.

+ 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] Macro to save file as CSV (Comma Delimited)
    By Jiptastic in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 11-09-2014, 03:23 PM
  2. Replies: 3
    Last Post: 03-29-2006, 04:55 PM
  3. Replies: 1
    Last Post: 05-03-2005, 06:06 PM
  4. [SOLVED] How do I save format changes in a comma delimited file?
    By mariam0673 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 03-01-2005, 02:06 PM
  5. [SOLVED] save an Excel worksheet as a comma delimited file?
    By Trophy Man in forum Excel General
    Replies: 2
    Last Post: 01-08-2005, 12:06 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