VBA to Split file into multiple CSV files based on Column

ajayscv

New Member
Joined
May 28, 2018
Messages
5
Hi All,

I have a file of received items in the company. I need to split the File into CSV files based on the Scanner Number (HHT Name). The main in this is like I would like to select the columns only which columns need to be exported in csv (from 3 -6 columns based on the Brand, with or without Header row

Please help.


BarcodeItem No.DescriptionVariant CodeHHT NameRack CodeQty.
9348989137333R03CBOCI7MIRAGE AGGROFLORAL BOARDSHORT0090301091418
9348989137333R03CBOCI7MIRAGE AGGROFLORAL BOARDSHORT0090301091014
9348989137333R03CBOCI7MIRAGE AGGROFLORAL BOARDSHORT009030111698
9348989137340R03CBOCI7MIRAGE AGGROFLORAL BOARDSHORT009032109146
9348989137340R03CBOCI7MIRAGE AGGROFLORAL BOARDSHORT0090321114130
9348989137340R03CBOCI7MIRAGE AGGROFLORAL BOARDSHORT0090321116916
9348989137340R03CBOCI7MIRAGE AGGROFLORAL BOARDSHORT009032113438
9348989137357R03CBOCI7MIRAGE AGGROFLORAL BOARDSHORT0090341113630
9348989137357R03CBOCI7MIRAGE AGGROFLORAL BOARDSHORT0090341117716
9348989137357R03CBOCI7MIRAGE AGGROFLORAL BOARDSHORT009034113436
9348989137364R03CBOCI7MIRAGE AGGROFLORAL BOARDSHORT009036109144
9348989137364R03CBOCI7MIRAGE AGGROFLORAL BOARDSHORT009036111778
9348989137364R03CBOCI7MIRAGE AGGROFLORAL BOARDSHORT00903611210211
9348989137364R03CBOCI7MIRAGE AGGROFLORAL BOARDSHORT009036113434
9348989137388R03CBOCI7MIRAGE AGGROFLORAL BOARDSHORT390128111406
9348989137395R03CBOCI7MIRAGE AGGROFLORAL BOARDSHORT390130109386
9348989137395R03CBOCI7MIRAGE AGGROFLORAL BOARDSHORT390130111389

<tbody>
</tbody>
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Try this
- assumes cell A1 contains the first header
- you will probably find that Variant Code values have lost their leading zeros
- is that a problem? (does Variant Code contain a leading apostrophe in your file?)

This code must be placed in the sheet code module. fldr = full path to folder where csv files are saved
(right-click sheet tab \ select View Code \ paste code in window on right)
Code:
Sub CreateCSV()
    Const fldr = "[COLOR=#ff0000]C:\folder\subFolder[/COLOR]"
    Dim fName As String, rng As Range, collHHT As New Collection, HHT As Variant, temp As Worksheet

    Set rng = Me.Range("A1").CurrentRegion
    On Error Resume Next        'required for collHHT.Add(when used in this way), SpecialCells & ShowAllData
    Application.DisplayAlerts = False: Application.ScreenUpdating = False
    
'create collection of unique values
    For Each HHT In rng.Resize(, 1).Offset(1, 4)
        collHHT.Add CStr(HHT.Value), CStr(HHT.Value)
    Next
    
'filter the data and create CSV files
    If Not AutoFilterMode Then cel.AutoFilter
    Set temp = Sheets.Add
        For Each HHT In collHHT
            temp.Cells.Clear
            rng.AutoFilter Field:=5, Criteria1:=HHT
            fName = fldr & "\" & HHT & Format(Now, " yy mm dd hhmm") & ".csv"
            rng.Offset(, 2).Resize(, 4).SpecialCells(xlCellTypeVisible).Copy temp.Cells(1)
            temp.Copy
                ActiveWorkbook.SaveAs Filename:=fName, FileFormat:=xlCSV
                ActiveWorkbook.Close
            Me.ShowAllData
        Next
    temp.Delete
    Set collHHT = Nothing
    Application.DisplayAlerts = True: Application.ScreenUpdating = True

End Sub
 
Last edited:
Upvote 0
Hi Yongle,

Thank you very much for helping me on this.

This code Works very fine. But there is a mistake from my side happened in the Question. i suppose to say like every time with the report provided by different departments, i need to select some columns say Barcode, Qty or Item Number, Variant code, Qty like this based on the situation. I am not sure if possible, i would like to select manually the columns to be exported. Same code will work very fine if ability to decide which columns to exported added
 
Upvote 0
Try this
- Message box appears with HHT in header. User asked if column required. Columns appear in reverse order

Code:
Sub CreateCSV()
    Const fldr = "C:\TestArea\CSV"
    Dim fName As String, c As Long, hdr As Range, rng As Range, collHHT As New Collection, HHT As Variant, temp As Worksheet
    Set rng = Me.Range("A1").CurrentRegion
    On Error Resume Next        'required for collHHT.Add(when used in this way), SpecialCells & ShowAllData
      
'create collection of unique values
    For Each HHT In rng.Resize(, 1).Offset(1, 4)
        If HHT.Value > "" Then collHHT.Add CStr(HHT.Value), CStr(HHT.Value)
    Next
    
'filter the data and create CSV files
    Application.DisplayAlerts = False: Application.ScreenUpdating = False
    If Not AutoFilterMode Then rng.Cells(1).AutoFilter
        For Each HHT In collHHT
        'select & copy fltered data
            rng.AutoFilter Field:=5, Criteria1:=HHT
            Set temp = Sheets.Add
            rng.SpecialCells(xlCellTypeVisible).Copy temp.Cells(1)
        'select columns for each filter
            Set hdr = temp.Range(rng.Resize(1).Address)
            For c = hdr.Count To 1 Step -1
                If MsgBox(UCase(hdr.Cells(1, c).Value), vbYesNo, HHT & "     (Yes/No)") <> vbYes Then temp.Columns(c).Delete
            Next c
        'create csv
            fName = fldr & "\" & HHT & Format(Now, " yy mm dd hhmm") & ".csv"
            temp.Copy
            ActiveWorkbook.SaveAs Filename:=fName, FileFormat:=xlCSV
            ActiveWorkbook.Close
            temp.Delete
            Me.ShowAllData
        Next
    Set collHHT = Nothing
    Application.DisplayAlerts = True: Application.ScreenUpdating = True

End Sub
 
Upvote 0
Hi Yongle,

Thank you for supporting me on this.

So far i made some changes in the Idea, Sorry for that.

But the idea got improved. May be you can amend a little bit in your Code to fit for my needs.

I have created a User Form to get the needed with the help of some other codes retrieved from internet. Please have a look at the codes and user form and advise.

The whole idea is like Double clicking the Headers in the Listbox 1 will select and copy the Header to ListBox 2 where you can adjust the order of the Columns with Up and Down command Buttons. Then Combobox1 will allow us to select the reference Column for Spliting (HHT in the initial request), and Combobox2 will select the starting row and Combobox3 will select the file type.

There is a function in the Code for selecting the Destination Folder ( GetFolder() ) which will be shooting while we press the Split button and will split the sheet into files to the selected folder.

Please see the attached Excel File in the following link for reference and advise how can we meet the needful from your code

www.ajayscv.ml

Thanks in Advance
 
Upvote 0

Forum statistics

Threads
1,214,639
Messages
6,120,679
Members
448,977
Latest member
dbonilla0331

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