Split One tab into Multiple Files with Specific Names

Jsubby3

New Member
Joined
Oct 24, 2016
Messages
14
Hello All,

I have been searching up and down trying to plagiarize various code to get this to work. I have a file that lists a series of data for all suppliers, weekly I would like to be able to split these into individual files by supplier and automatically name them as the Supplier name. An example of my data is below, header starts in cell A5 and goes to R5 - obviously would need the headers to be in each individual file. Any help would be greatly, greatly appreciated

Purch. OrganizationPlantVendorNamePurchasing DocumentItemPurchasing Doc. TypePurchasing GroupMaterialMaterial DescriptionSchedule LineScheduled QuantityPO UOMReceived Qty Purch UOMOpen Qty Purch UOMDelivery DateStat.-Rel. Del. DateDelivery Completed
10001000214Supplier A6474120NB001part 121.000M20.0001.0009/30/20179/30/2017
10001000432Supplier B12320NB001part 211.000M20.00019/30/20179/30/2017

<tbody>
</tbody>
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Your system obviously works different to mine, as when I create a new workbook, it automatically becomes the active book.
See if this fixes the problem
Code:
Sub FltrCopy()

    Dim Dict As Object
    Dim Ky As Variant
    Dim Cl As Range
    Dim UsdRws As Long
    Dim Wbk As Workbook

Application.ScreenUpdating = False
    Set Dict = CreateObject("scripting.dictionary")

    With Sheets("Data List")
        UsdRws = .Range("D" & Rows.Count).End(xlUp).Row
    
        For Each Cl In .Range("D6:D" & UsdRws)
            If Not Dict.exists(Cl.Value) Then Dict.Add Cl.Value, Nothing
        Next Cl
    
        For Each Ky In Dict.keys
            Set Wbk = Workbooks.Add(1)
            Wbk.Sheets(1).Name = Ky
            .Range("A5").AutoFilter Field:=4, Criteria1:=Ky
            .Range("A5:R" & UsdRws).SpecialCells(xlVisible).Copy Wbk.Sheets(Ky).Range("A1")
            Wbk.SaveAS "C:\Users\Davec\Desktop\test\" & Ky, 51
            Wbk.Close
        Next Ky
        .AutoFilterMode = False
    End With

End Sub
 
Upvote 0
No worries, glad to help & thanks for the feedback
 
Upvote 0
I was feeling awesome about this until I expanded my data and ran into an issue. The Macro works as intended but it is naming the tab by the vendor name, nice touch however some of my supplier names exceed the maximum amount of characters allowed by Excel so the loop fails when it hits one of those suppliers. I tried a number of things but haven't been able to change your code to correct it.
 
Upvote 0
OK, one option is this
Code:
Sub FltrCopy()

    Dim Dict As Object
    Dim Ky As Variant
    Dim Cl As Range
    Dim UsdRws As Long
    Dim Wbk As Workbook

Application.ScreenUpdating = False
    Set Dict = CreateObject("scripting.dictionary")

    With Sheets("Data List")
        UsdRws = .Range("D" & Rows.Count).End(xlUp).Row
    
        For Each Cl In .Range("D6:D" & UsdRws)
            If Not Dict.exists(Cl.Value) Then Dict.Add Cl.Value, Nothing
        Next Cl
    
        For Each Ky In Dict.keys
            Set Wbk = Workbooks.Add(1)
            [COLOR=#0000ff]If Len(Ky) > 31 Then
                Wbk.Sheets(1).Name = Left(Ky, 31)
            Else
                Wbk.Sheets(1).Name = Ky
            End If[/COLOR]
            .Range("A5").AutoFilter Field:=4, Criteria1:=Ky
            .Range("A5:R" & UsdRws).SpecialCells(xlVisible).Copy Wbk.Sheets(1).Range("A1")
            Wbk.SaveAS "C:\Users\Fluff\Desktop\test\" & Ky, 51
            Wbk.Close
        Next Ky
        .AutoFilterMode = False
    End With

End Sub
The lines in blue will name the sheet with the vendors name & if the name is more then 31 characters, then it will truncate the name to the first 31 characters.
Another option, simply delete the lines in blue & the sheet will use the default name of Sheet1
 
Upvote 0
Hi All! Why it doesn't save me in excel 2016????

OK, one option is this
Code:
Sub FltrCopy()

    Dim Dict As Object
    Dim Ky As Variant
    Dim Cl As Range
    Dim UsdRws As Long
    Dim Wbk As Workbook

Application.ScreenUpdating = False
    Set Dict = CreateObject("scripting.dictionary")

    With Sheets("Data List")
        UsdRws = .Range("D" & Rows.Count).End(xlUp).Row
    
        For Each Cl In .Range("D6:D" & UsdRws)
            If Not Dict.exists(Cl.Value) Then Dict.Add Cl.Value, Nothing
        Next Cl
    
        For Each Ky In Dict.keys
            Set Wbk = Workbooks.Add(1)
            [COLOR=#0000ff]If Len(Ky) > 31 Then
                Wbk.Sheets(1).Name = Left(Ky, 31)
            Else
                Wbk.Sheets(1).Name = Ky
            End If[/COLOR]
            .Range("A5").AutoFilter Field:=4, Criteria1:=Ky
            .Range("A5:R" & UsdRws).SpecialCells(xlVisible).Copy Wbk.Sheets(1).Range("A1")
            Wbk.SaveAS "C:\Users\Fluff\Desktop\test\" & Ky, 51
            Wbk.Close
        Next Ky
        .AutoFilterMode = False
    End With

End Sub
The lines in blue will name the sheet with the vendors name & if the name is more then 31 characters, then it will truncate the name to the first 31 characters.
Another option, simply delete the lines in blue & the sheet will use the default name of Sheet1
 
Upvote 0
@Mandy_84
Could you please be a bit more specific? I don't understand what you are saying.
 
Upvote 0

Forum statistics

Threads
1,216,007
Messages
6,128,244
Members
449,435
Latest member
Jahmia0616

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