Incremental selection and data export to csv using VBA

cathairs

New Member
Joined
Dec 27, 2015
Messages
2
Hi guys,

I am a very new user to VBA and am trying to utilise a macro to help with a batch export from a very large dataset.

The dataset is 1872 columns wide and 1285 rows. What I am trying to achieve is to do a batch selection and export to csv of the dataset. I am trying to select columns 1:6 and export as a csv, then select 7:12 and export as a csv etc until the end of the dataset.

So the end result is that I should have 312 csv files.

I have tried to record a macro to complete the process for the first csv, but am unsure how to then increment by data selection by 6 columns and loop it until the end.

if anyone has some tips or comments it would be really appreciated.

My code is below:
Code:
Sub Macro2()
'
' Macro2 Macro
'

'
    Columns("A:F").Select
    Selection.Copy
    Sheets("Sheet1").Select
    Range("A1").Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
    ChDir "C:\Users\Glen\AppData\Roaming\Microsoft\Templates"
    ActiveWorkbook.SaveAs Filename:= _
        "C:\Users\Glen\AppData\Roaming\Microsoft\Templates\EMN_001.csv", FileFormat:= _
        xlCSV, CreateBackup:=False
End Sub
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Sheets(1) is the sheet with data.
Sheets(2) - must be second sheet from left.
Feel free to change names and location to suit your structure.

Code:
Sub Macro2()

    Dim i, c
    Application.ScreenUpdating = False    
    For c = 1 To 1872 Step 6
        i = i + 1
        Sheets(1).Cells(1, c).Resize(1285, 6).Copy Sheets(2).Cells(1)
        Application.CutCopyMode = False
        Sheets(2).SaveAs Filename:= _
            "C:\Users\Glen\AppData\Roaming\Microsoft\Templates\EMN_00" & i & ".csv", FileFormat:=xlCSV
    Next
    
End Sub
 
Upvote 0
A slightly different take on the code Sektor posted.
Sheet layout is the same as in Sektor's code.

Rich (BB code):
Sub ToCsv2()
    Dim i As Long, c As Long, j As String, wb As Workbook, lr As Long
    Application.ScreenUpdating = False
    
    For c = 1 To Sheets(1).Cells(1, Columns.Count).End(xlToLeft).Column Step 6
        
        i = i + 1
        lr = ThisWorkbook.Sheets(1).Cells.Find(what:="*", SearchOrder:=xlRows, SearchDirection:=xlPrevious, LookIn:=xlValues).Row
        
        ThisWorkbook.Sheets(1).Cells(1, c).Resize(lr, 6).Copy ThisWorkbook.Sheets(2).Cells(1)
        ThisWorkbook.Sheets(2).Copy

        Set wb = ActiveWorkbook
        
        With wb
            .SaveAs Filename:= _
                    "C:\Users\Glen\AppData\Roaming\Microsoft\Templates\EMN_" & Format(i, "000") & ".csv", FileFormat:=xlCSV
            .Close False
        End With
    
    Next
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Hi Sektor and MARK858,

I just wanted to thank you both very much for your code. I have tried both instances and they work perfectly.

Thanks very much for your time and help. This is such a good online community on this forum.

I'm learning a lot about VBA now and will continue working on more code!

Thanks again to both of you.
 
Upvote 0

Forum statistics

Threads
1,215,477
Messages
6,125,037
Members
449,205
Latest member
Eggy66

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top