+ Reply to Thread
Results 1 to 11 of 11

Macro to open workbooks, process data, save copy as a csv

  1. #1
    Registered User
    Join Date
    02-14-2013
    Location
    USA
    MS-Off Ver
    2010 Pro
    Posts
    13

    Macro to open workbooks, process data, save copy as a csv

    Hello all, I'm running into a bit of an issue with saving a csv using VBA, hoping someone here can save me again!

    I've got a few sub scripts that are used to process the data but the part I'm stuck on is saving a copy of the current workbook as a CSV without closing the current Workbook.

    The excel file containing the macro is a blank worksheet, "Importer". The macro will loop through 61 excel CSV workbooks, copy the data from them to the main excel workbook's worksheet, "Importer". At this point macros are ran to process the data. Once complete, I want to take the remaining data and save it as a new .csv file. I am using this code currently:

    ActiveWorkbook.SaveAs Filename:="C:\Scrubbed\" & CurBookSaveStr, FileFormat:=xlCSV, _
    CreateBackup:=False

    However, it seems to save the currently opened WB with the designated name, thus I lose my original WB containing my macro (and I fear I will lose my loop that will be put in place to process 61 csv files in this same way).

    ActiveWorkbook.SaveCopyAs Filename:="C:\Scrubbed\" & CurBookSaveStr does save a copy of the file without closing my original, but it does not let me save it as a csv just xls.. (if I force the file name to be .csv, it's not actually a .csv file)

    Can anyone make an suggestions regarding the best way to save this file?

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

    Re: Macro to open workbooks, process data, save copy as a csv

    paste here the complete code
    If solved remember to mark Thread as solved

  3. #3
    Registered User
    Join Date
    02-14-2013
    Location
    USA
    MS-Off Ver
    2010 Pro
    Posts
    13

    Re: Macro to open workbooks, process data, save copy as a csv

    I cannot release all of the details for the ScrubForImport Macro, however, it's code executes correctly and is not involved with the opening or saving of the files. All the data I am trying to automate is saved in csv files called wb1.csv, wb2.csv, etc up to wb61.csv. I'm trying to run this same ScrubForImport macro on all of them and save them all as new .csv files.
    The main Macro that gets run:
    Please Login or Register  to view this content.
    This is the OpenWorkBooks Macro:
    Please Login or Register  to view this content.
    This is the SaveWorkBooks Macro:
    Please Login or Register  to view this content.

  4. #4
    Valued Forum Contributor xlbiznes's Avatar
    Join Date
    02-22-2013
    Location
    Bahrain
    MS-Off Ver
    Excel 2007
    Posts
    1,223

    Re: Macro to open workbooks, process data, save copy as a csv

    Hi,

    try this.


    Please Login or Register  to view this content.
    Happy Computing ,

    Xlbiznes.

    To show your appreciation please click *

  5. #5
    Registered User
    Join Date
    02-14-2013
    Location
    USA
    MS-Off Ver
    2010 Pro
    Posts
    13

    Re: Macro to open workbooks, process data, save copy as a csv

    Xlbiznes, I attempted to implement that code but I get an error. "Compile error: Method or data member not found"
    then it point to xl_wrk.Close
    I changed CSV_DATA tp CurWorkSheetStr because all sheets are the same as the WB name (wb1.csv = wb1, wb2.csv = wb2 etc)
    Am I supposed to do something with that "sheet1" part?

  6. #6
    Valued Forum Contributor xlbiznes's Avatar
    Join Date
    02-22-2013
    Location
    Bahrain
    MS-Off Ver
    Excel 2007
    Posts
    1,223

    Re: Macro to open workbooks, process data, save copy as a csv

    Hi,

    Try this and change the variables to the ones that you are using in your sheet.

    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    02-14-2013
    Location
    USA
    MS-Off Ver
    2010 Pro
    Posts
    13

    Re: Macro to open workbooks, process data, save copy as a csv

    Please Login or Register  to view this content.
    is giving a run-time error 9 subscript out of range now. What would you advise next? Thank you very much for your help so far, it is much appreciated!

  8. #8
    Valued Forum Contributor xlbiznes's Avatar
    Join Date
    02-22-2013
    Location
    Bahrain
    MS-Off Ver
    Excel 2007
    Posts
    1,223

    Re: Macro to open workbooks, process data, save copy as a csv

    Hi,

    Can you try by changing this line Set wrk = xcl.Worksheets(CurWorkSheetStr) to Set wrk = xcl.Worksheets("Sheet1").

    If this does not work , please post the code for Sub SaveWorkBooks() that you have implemented after making the changes that i had recommended.

  9. #9
    Registered User
    Join Date
    02-14-2013
    Location
    USA
    MS-Off Ver
    2010 Pro
    Posts
    13

    Re: Macro to open workbooks, process data, save copy as a csv

    Hey Xlbiznes, I tried the proposed changes, "Compile Error: Method or data member not found"
    Here is what I have currently

    Please Login or Register  to view this content.

  10. #10
    Valued Forum Contributor xlbiznes's Avatar
    Join Date
    02-22-2013
    Location
    Bahrain
    MS-Off Ver
    Excel 2007
    Posts
    1,223

    Re: Macro to open workbooks, process data, save copy as a csv

    Hi,

    You need to change this code xl_wrk.Close to xcl.Close.

    xcl is the workbook that you are creating and saving as a csv file and this needs to be closed. Hope this will solve the issue.

  11. #11
    Registered User
    Join Date
    02-14-2013
    Location
    USA
    MS-Off Ver
    2010 Pro
    Posts
    13

    Re: Macro to open workbooks, process data, save copy as a csv

    Hey Xlbiznes,

    It took me a while to actually figure out what the code was trying to accomplish. Once I finally made sense of it I was able to tweek it to my needs. Fortunately, the code is working! I'd post the code that finally got it working but the macro is currently running so I can access it! It's currently processing 61 workbooks that are approx. 100mb each, performing about 10 minutes of sanitizing the data per sheet. I'm on a core2quad 2.40Ghz w/ 4GB of RAM. This process is using about 2.5 and up to 3.0gb of RAM and would normally require my intervention about every 10-15 minutes. Now, its doing it's thing while I write this. Thank you so much for you help Xlbiznes!!!!

    -Dan

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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