+ Reply to Thread
Results 1 to 11 of 11

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

Hybrid View

  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:
    Public TotalQtySold As Long, BookCount As Integer, CurrentBook As Integer, CurBookStr As String, CurWorkSheetStr As String, CurBookSaveStr As String
    Sub AutoScrub()
    'This sub will read through all workbooks for a bulk import. They are named wb1.csv, wb2.csv etc.
    BookCount = 2
    CurrentBook = 1
    CurBookStr = "wb" & CurrentBook & ".csv"
    CurWorkSheetStr = "wb" & CurrentBook
    CurBookSaveStr = "wb" & CurrentBook & "_scrubbed.csv"
    
    'Loop through all csv files to scrub
    For CurrentBook = 1 To BookCount
    Call OpenWorkBooks
    Call ScrubForImport
    Call SaveWorkBooks
    CurrentBook = CurrentBook + 1
    Next
    
    End Sub
    This is the OpenWorkBooks Macro:
    Sub OpenWorkBooks()
    'Opens all workbooks in a folder, copies their contents to this workbook, scrubs the data and saves it as a csv for import into Access
    Dim wsopen As Worksheet
    'Manually set BookCount here
    Workbooks.Open Filename:="C:\Exports\" & CurBookStr
    MsgBox ("WB Opened")
        
        'Select and Copy sheet from a RIS CSV export
        Workbooks(CurBookStr).Activate
        Worksheets(CurWorkSheetStr).Activate
        ActiveSheet.Cells.Copy
        MsgBox ("WS Copied")
        
        'Select and Paste sheet to BulkProcessor.xlsm
        Workbooks("BulkProcessor.xlsm").Activate
        Worksheets("Import").Activate
        Range("A1").Select
        ActiveSheet.Paste
        MsgBox ("WS Pasted")
        
        'Close other WB to save RAM
        Workbooks(CurBookStr).Close
        MsgBox ("WB Closed")
        
    End Sub
    This is the SaveWorkBooks Macro:
    Sub SaveWorkBooks()
    'This sub will save the scrubbed data as a CSV for import into Access
    Application.DisplayAlerts = False
    ActiveWorkbook.SaveAs Filename:="C:\Scrubbed\" & CurBookSaveStr, FileFormat:=xlCSV, _
    CreateBackup:=False
    'ActiveWorkbook.SaveCopyAs Filename:="C:\Scrubbed\" & CurBookSaveStr, FileFormat:=xlCSVWindows
    End Sub

  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.


    Sub SaveWorkBooks()
    'This sub will save the scrubbed data as a CSV for import into Access
    Dim xl_wrk As Worksheet
    Dim xcl As New Workbook
    Set xcl = Workbooks.Add
    Dim wrk As New Worksheet
    
        Set wrk = xcl.Worksheets("sheet1")
        Set xl_wrk = ThisWorkbook.Worksheets("CSV_DATA") ' change this to the sheet which contains your csv data
        xl_wrk.UsedRange.Copy
        wrk.Paste
        Application.DisplayAlerts = False
        xcl.SaveAs Filename:="C:\Scrubbed\" & CurBookSaveStr, FileFormat:=xlCSV, _
        CreateBackup:=False
        xl_wrk.Close
        Application.DisplayAlerts = True
        Selection.Clear
    
    Application.DisplayAlerts = False
    'ActiveWorkbook.SaveCopyAs Filename:="C:\Scrubbed\" & CurBookSaveStr, FileFormat:=xlCSVWindows
    End Sub
    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.

    Sub SaveWorkBooks()
    'This sub will save the scrubbed data as a CSV for import into Access
    Dim xl_wrk As Worksheet
    Dim xcl As New Workbook
    Set xcl = Workbooks.Add
    Dim wrk As New Worksheet
    
    CurBookSaveStr = "test"
    
        Set wrk = xcl.Worksheets("sheet1")
        Set xl_wrk = ThisWorkbook.Worksheets("sheet1") ' change this to the sheet which contains your csv data
        xl_wrk.UsedRange.Copy
        wrk.Paste
        Application.DisplayAlerts = False
        xcl.SaveAs Filename:="C:\" & CurBookSaveStr, FileFormat:=xlCSV, _
        CreateBackup:=False
        
       
        Selection.Clear
        
        xcl.Close
        
     Application.DisplayAlerts = True
    
    'ActiveWorkbook.SaveCopyAs Filename:="C:\Scrubbed\" & CurBookSaveStr, FileFormat:=xlCSVWindows
    End Sub

  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

    Set wrk = xcl.Worksheets(CurWorkSheetStr)
        Set xl_wrk = ThisWorkbook.Worksheets(CurWorkSheetStr) ' change this to the sheet which contains your csv data
    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

    Sub SaveWorkBooks()
    'This sub will save the scrubbed data as a CSV for import into Access
    Dim xl_wrk As Worksheet
    Dim xcl As New Workbook
    Set xcl = Workbooks.Add
    Dim wrk As New Worksheet
    
        Set wrk = xcl.Worksheets("Sheet1")
        Set xl_wrk = ThisWorkbook.Worksheets("Sheet1") ' change this to the sheet which contains your csv data
        xl_wrk.UsedRange.Copy
        wrk.Paste
        Application.DisplayAlerts = False
        xcl.SaveAs Filename:="C:\Scrubbed\" & CurBookSaveStr, FileFormat:=xlCSV, _
        CreateBackup:=False
        xl_wrk.Close
        Application.DisplayAlerts = True
        Selection.Clear
    
    Application.DisplayAlerts = False
    
    End Sub

  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