+ Reply to Thread
Results 1 to 12 of 12

batch opening CSV and saving CSV

  1. #1
    Registered User
    Join Date
    03-21-2013
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    6

    Question batch opening CSV and saving CSV

    Hello,
    I am a complete novice to excel macros, and I would appreciate some help with a simple problem.

    An instrument in our lab saves its data as CSV files. These are openable in MS Excel, but not other programs we use for data analysis (such as Matlab or Origin). If I open the file in excel, then save it (still as a csv) (though excel seems to default to unicode text when you go to resave it) it magically becomes readable by our other programs. I would like write a macro to open all files and resave them as CSV. All my data files have the format 1letters#.csv. So I can tell if the files have been resaved, I would like to save the new files as "A*.csv". So the original file might be 1abcA1.csv. After it has been resaved, it should be A1abcA1.csv.

    I found a thread here: http://www.excelforum.com/excel-gene...rnal-data.html

    which looks like it should be relevant. But, I think the loop is using the extension as the condition- since I am saving the file under the same extension, I think maybe it will cause an endless loop. However, I don't get that far. The modified code won't even compile.

    Please Login or Register  to view this content.
    When I record a macro to open and resave a single file, it looks very different from the sample code given in the previous thread. This is what I get:

    Please Login or Register  to view this content.
    I'm not sure what the difference is between OpenText and QueryTables.Add. I'm using excel 2007. The code in the thread above was for 2003. Is that the difference?

    Thanks for your help.
    ssbbg
    Last edited by ssbbg; 03-25-2013 at 02:03 PM.

  2. #2
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: batch opening CSV and saving CSV

    there are many ways to get the same result, does the first code work ? you can use it
    If solved remember to mark Thread as solved

  3. #3
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: batch opening CSV and saving CSV

    Hi, ssbbg,

    AFAIK the macro code should read
    Please Login or Register  to view this content.
    instead of
    Please Login or Register  to view this content.
    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  4. #4
    Registered User
    Join Date
    03-21-2013
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    6

    Re: batch opening CSV and saving CSV

    Thanks! Ha! Ha! Funny mistake- That was Matlab slipping through- strings are denoted with 'string' instead of "string". I did catch that when the forum was down, but the CSV file is not opening correctly. There was also an extra loop, and workbook.add instead of workbooks.add

    I have commented out the loop part, and am just trying to get the open and close part to work (from the original thread)

    I now have this:
    Please Login or Register  to view this content.
    So, it does open and save a file, but it completely mangles it. I cannot find out how to tell it to understand that the file is comma delimited. I looked up "QueryTables.Add" and "connection" in the help, and it is pretty opaque. Nothing seems related to the setting a delimiter. All the subsequent .text stuff below the width also does not seem related.

    Thanks again.
    ssbbg

  5. #5
    Registered User
    Join Date
    03-21-2013
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    6

    Re: batch opening CSV and saving CSV

    Quote Originally Posted by patel45 View Post
    there are many ways to get the same result, does the first code work ? you can use it
    No, it doesn't work. I'm not sure why. Thanks.

  6. #6
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: batch opening CSV and saving CSV

    Hi, ssbbg,

    maybe
    Please Login or Register  to view this content.
    Maybe you attach a file to check the macro both as an original and as a wanted outcome.

    Ciao,
    Holger
    Last edited by HaHoBe; 03-25-2013 at 01:43 PM.

  7. #7
    Registered User
    Join Date
    03-21-2013
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    6

    Re: batch opening CSV and saving CSV

    Holger,
    Thank you! I don't know how I missed that. Also, the property needs to be ".TextFileParseType = xlDelimited" from fixed width.

    Now to try looping, which may be extra fun.

    Quote Originally Posted by HaHoBe View Post
    Hi, ssbbg,

    maybe
    Please Login or Register  to view this content.
    Maybe you attach a file to check the macro both as an original and as a wanted outcome.

    Ciao,
    Holger

  8. #8
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: batch opening CSV and saving CSV

    Please Login or Register  to view this content.

  9. #9
    Registered User
    Join Date
    03-21-2013
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    6

    Re: batch opening CSV and saving CSV

    Holger,
    The loop worked perfectly. Thank you for all your help.

    For completeness, here is my final code that works.
    Please Login or Register  to view this content.

  10. #10
    Registered User
    Join Date
    03-21-2013
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    6

    Re: batch opening CSV and saving CSV

    Holger,
    The loop worked perfectly. Thank you for all your help.

    For completeness, here is my final code that works.
    Please Login or Register  to view this content.

  11. #11
    Registered User
    Join Date
    03-21-2013
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    6

    Re: batch opening CSV and saving CSV

    Thank you Patel 45. I will study this carefully, as this looks more similar to what I got when I recorded the macro for opening a single file. What is the advantage of OpenText vs. QueryTables.Add?

    Quote Originally Posted by patel45 View Post
    Please Login or Register  to view this content.

  12. #12
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: batch opening CSV and saving CSV

    this looks more similar to what you got when you recorded the macro for opening a single file because I copied it

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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