[Macro] how can I filter and save separate .csv files ?

dario2018

New Member
Joined
May 21, 2018
Messages
1
Hellooooo
I know nothing about VBA/Macro, I'm looking to have multiple .csv files, filtered by column (B) value.

example:
___A____|___B____|___C____|___D____|
0000001___01_____Rod_S12____9234
0000001___01_____Red_345____1244
0000001___02_____XYZ_A1_____4521
0000001___02_____KMO123_____8511


So I'm expecting to have separate files by column B filter, this way, as much files as "groups" in column B:

File "01.csv"
___A____|___B____|___C____|___D____|
0000001___01_____Rod_S12____9234
0000001___01_____Red_345____1244


File "02.csv"
___A____|___B____|___C____|___D____|
0000001___02_____XYZ_A1_____4521
0000001___02_____KMO123_____8511



and so on......I've from 02 to 30.....
Can you help me PLEASE?? :);)
 

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
Welcome to MrExcel forums.

See if this works for you. The .csv files are created in the same folder as the active workbook.

Code:
Public Sub Create_CSV_Files()

    Dim allData As Variant
    Dim r As Long
    Dim fileNum As Integer
    
    With ActiveWorkbook.Worksheets(1)
        r = .Cells(.Rows.Count, "B").End(xlUp).Row
        If Not IsEmpty(.Cells(r, "B").Value) Then r = r + 1
    End With
        
    allData = Worksheets("Sheet1").Range("A1:D" & r).Value  'last array row is empty to facilitate loop below
        
    r = 0
    While r < UBound(allData, 1) - 1
        fileNum = FreeFile
        Open ActiveWorkbook.Path & "\" & allData(r + 1, 2) & ".csv" For Output As [URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=fileNum"]#fileNum[/URL] 
        Do
            r = r + 1
            Print [URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=fileNum"]#fileNum[/URL] , Join(Application.WorksheetFunction.Index(allData, r, 0), ",")
        Loop While allData(r, 2) = allData(r + 1, 2)
        Close [URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=fileNum"]#fileNum[/URL] 
    Wend
    
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,920
Messages
6,175,377
Members
452,638
Latest member
Oluwabukunmi

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