Split Excel file down based on value in column

EmilyNL

New Member
Joined
Dec 8, 2017
Messages
9
Hi all

I have a file with 22.000 lines. They contain our stock codes plus their suppliers (plus some more information but not relevant for this thread). I want to create a separate excel file (or CSV, whatever I can get) for each supplier as there are over 300 suppliers.

I suppose someone with macro skills would be able to do this? So I know how to start recording and stop recording and enter the shortcut, how far can I get with this?
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Hi & welcome to the board.

I think you'll struggle to record a macro for this, but if you can answer the following I'll have a look at it.
1) What is the name of sheet containing the data?
2) What column holds the supplier name?
3) Do you have a header in row, with data starting in A2? If not where is your data?
4) Do you want the new files saved to the same directory as the original file?
 
Upvote 0
Hi & welcome to the board.

I think you'll struggle to record a macro for this, but if you can answer the following I'll have a look at it.
1) What is the name of sheet containing the data?
2) What column holds the supplier name?
3) Do you have a header in row, with data starting in A2? If not where is your data?
4) Do you want the new files saved to the same directory as the original file?

Thanks Fluff, this will be for Monday or Tuesday as I am desperate to get out of the office after a 60 hour workweek ;).
 
Upvote 0
Hey!

So, to answer your questions:

1) What is the name of sheet containing the data?
Stocked items SE+NL

2) What column holds the supplier name?
Column D

3) Do you have a header in row, with data starting in A2? If not where is your data?
Header in row 1, data starting in A2

4) Do you want the new files saved to the same directory as the original file?
Yes, that will be fine!

Thanks in advance, Fluff!
 
Upvote 0
Ok try this
Code:
Sub SplitsheetToWorkbooks()

   Dim SrcWs As Worksheet
   Dim wbk As Workbook
   Dim Cl As Range
   
Application.ScreenUpdating = False

   Set SrcWs = Sheets("Stocked items SE+NL")
   
   If SrcWs.AutoFilterMode Then SrcWs.AutoFilterMode = False
   
   With CreateObject("Scripting.dictionary")
      For Each Cl In SrcWs.Range("D2", SrcWs.Range("D" & Rows.Count).End(xlUp))
         If Not .exists(Cl.Value) Then
            .Add Cl.Value, Nothing
            SrcWs.Range("A1").AutoFilter 4, Cl.Value
            Set wbk = Workbooks.Add(1)
            SrcWs.Range("A1").CurrentRegion.SpecialCells(xlVisible).copy wbk.Sheets(1).Range("A1")
            wbk.SaveAs ThisWorkbook.Path & "\" & Cl.Value, [COLOR=#ff0000]51[/COLOR]
            wbk.Close False
         End If
      Next Cl
   End With
   SrcWs.AutoFilterMode = False

End Sub
This will save the files in xlsx format, if you prefer xlsm format, then change the red 51 to 52
 
Upvote 0
Ok try this
Code:
Sub SplitsheetToWorkbooks()

   Dim SrcWs As Worksheet
   Dim wbk As Workbook
   Dim Cl As Range
   
Application.ScreenUpdating = False

   Set SrcWs = Sheets("Stocked items SE+NL")
   
   If SrcWs.AutoFilterMode Then SrcWs.AutoFilterMode = False
   
   With CreateObject("Scripting.dictionary")
      For Each Cl In SrcWs.Range("D2", SrcWs.Range("D" & Rows.Count).End(xlUp))
         If Not .exists(Cl.Value) Then
            .Add Cl.Value, Nothing
            SrcWs.Range("A1").AutoFilter 4, Cl.Value
            Set wbk = Workbooks.Add(1)
            SrcWs.Range("A1").CurrentRegion.SpecialCells(xlVisible).copy wbk.Sheets(1).Range("A1")
            wbk.SaveAs ThisWorkbook.Path & "\" & Cl.Value, [COLOR=#ff0000]51[/COLOR]
            wbk.Close False
         End If
      Next Cl
   End With
   SrcWs.AutoFilterMode = False

End Sub
This will save the files in xlsx format, if you prefer xlsm format, then change the red 51 to 52

Awesome, I can see the files starting to come in now! You're a genius!
I had a question, was I supposed to filter alphabetically or will it automatically group? I'm sure I will know in the next hour anyway, in which case I will do this again ;).
 
Upvote 0
No need to sort of filter the data, the macro will do that itself.
That said if you sort on Col D first, the macro might be a bit quicker.
 
Upvote 0
So just an issue that I encountered. Some of my suppliers have a . in their official name, like Fake Name s.p.a. It will then save as an .A file, LOL. So I have countered this by replacing all dots for space in the data. I didn't filter alphabetically in the end, it works like a charm!
 
Upvote 0
If this is something you'll be doing on a regular basis, I can easily modify the code to do the replace as well.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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