Consulting

Results 1 to 6 of 6

Thread: Selecting sets of 21 Rows and saving as a csv

  1. #1
    VBAX Regular
    Joined
    Aug 2014
    Posts
    7
    Location

    Selecting sets of 21 Rows and saving as a csv

    Hello,

    I have one of those projects that I thought would be really easy and turns out that it would be a lot harder than I thought!

    I have a spreadsheet which has about 1000 rows in and I need to copy 21 rows at a time and then save those 21 rows it as a separate csv file. I'm struggling to find the best way to copy the 21 rows and then copy the next 21 rows, if you could point me in the right direction that would be really helpful!

    Thanks

  2. #2
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    Sub M_snb()
      sn=thisworkbook.sheet1.cells(1).currentregion
      sp=evaluate("transpose(row(1:" & ubound(sn,2) & "))")
    
      with workbooks.add
         for j=1 to ubound(sn) step 21
            .sheet1.cells(1).resize(21,ubound(sn,2))= application.index(sn, evaluate("row(" j & ":" & j+21 & ")"),sp)
            .saveas "G:\OF\file " & j & ".csv",21
         next
         .close 0
      end with
    End sub

  3. #3
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    EDIT: i sometimes forget refreshing the page to see if any solution is posted.

    _______________________________________________________________

    hi.
    try this.

    saves csv files in the same directory...
    Sub vbax_54140_Save_Every_N_Rows_As_Separate_CSV_Files()
        
        Dim i As Long, EveryNRows As Long
        
        EveryNRows = 21
        
        With Application
            .DisplayAlerts = False
            .ScreenUpdating = False
            .EnableEvents = False
        End With
        
        With ActiveSheet.Cells(1).CurrentRegion
            For i = 1 To .Rows.Count Step EveryNRows
                .Cells(i, 1).Resize(EveryNRows, .Columns.Count).Copy
                Workbooks.Add(xlWBATWorksheet).Sheets(1).Cells(1).PasteSpecial
                With ActiveWorkbook
                    .SaveAs ThisWorkbook.Path & "\Rows_" & i & "_" & (i + EveryNRows - 1) & ".csv", xlCSV
                    .Close False
                End With
            Next i
        End With
    
    
        With Application
            .EnableEvents = True
        End With
    
    End Sub
    Last edited by mancubus; 10-29-2015 at 04:28 AM. Reason: explanation added...
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  4. #4
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    @mancubus

    I think 1 new workbook should be sufficient..(also in your method)

    Sub M_snb()
       With Workbooks.Add
         For j = 1 To ThisWorkbook.Sheets(1).Cells(1).CurrentRegion.Rows.Count Step 21
            Intersect(ThisWorkbook.Sheets(1).Cells(1).CurrentRegion, Rows(j).Resize(21)).Copy .Sheets(1).Cells(1)
            .SaveAs "G:\OF\file_" & j & ".csv", 23
         Next
         .Close 0
       End With
    End Sub
    and this might be much faster:

    Sub M_snb()
       sn = ThisWorkbook.Sheets(1).Cells(1).CurrentRegion
       
       For j = 1 To UBound(sn) Step 21
         c00 = ""
         For jj = 1 To 21
            c00 = c00 & vbLf & Join(Application.Index(sn, j), "_")
         Next
         CreateObject("scripting.filesystemobject").createtextfile("G:\OF\file_" & j & ".csv").write Mid(c00, 2)
       Next
    End Sub

  5. #5
    VBAX Regular
    Joined
    Aug 2014
    Posts
    7
    Location
    Perfect! That makes my life so much easier

  6. #6
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    @snb
    you are right. i'll amend the code in my 'archived' workbook to suit yours.

    @Emmerly
    you are welcome.
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •