Macro to create new workbooks from current workbook data

Dramsey

New Member
Joined
Sep 15, 2015
Messages
3
I have a table with 7 columns. Cust Id, Customer name, cust part number, cust description, our part number, price, uom. I need to create a workbook for each customer (782 total workbooks/customers) that has columns 3 to 7, each customer has multiple rows for each item(over 16000 rows), but no 2 customers necessarily share the same product selection. Also need this with the headers and saved as cust id for filename. Can a macro do this automatically?
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
GroupFirstOfCustomerFirstOfItem NumberFirstOfSC New Item CombinedFirstOfWatts_DescImport_NetFirstOfUM_Conv_FactorFirstOfUM
B J PLUMBINGB J PLUMBING SUPPLY0123936637-B23HWPWatts LFA-158A 3/8Cx3/4 Elbow$1EA
B J PLUMBINGB J PLUMBING SUPPLY0123936637-B23HWPWatts LFA-158A 3/8Cx3/4 Elbow$1EA
B&D003343100121009134-G2S0CR104Watts LF892151$1EA
B&D003343100147033134-G2S1CE504Watts LF892503$1EA
B&D003343100147097133-G2S1CE504Watts LF892513$1EA
B&D003343100147099133-G2S1CR104Watts LF892011$1EA
B&G SPECIALTYB & G SPECIALITY COMPANY0142388696WVH12Watts 1/2 175C WASHING MACH$1EA
B&G SPECIALTYB & G SPECIALITY COMPANY0142388696WVH12Watts 1/2 175C WASHING MACH$1EA

<tbody>
</tbody>
 
Last edited:
Upvote 0
Welcome to MrExcel forums.

A few pointers to do this: get a list of unique customer ids (either using Advanced Filter on the Cust Id column to put unique Cust Ids in a temporary sheet, or loop through the Cust Ids adding them to a VBA Collection or Dictionary object, to get a list of unique Cust Ids); then for each unique Cust Id, copy the main sheet to a separate sheet, then advanced filter or autofilter against that Cust Id, delete unwanted columns 1-2, save the sheet as a workbook with Cust Id as the file name. Start by recording macros to do each part of the process, and search this forum for the bits of code to glue it all together.
 
Upvote 0
Here it is for anyone else that may be interested

Code:
Sub TopFolder()

Set myFolder = Application.FileDialog(msoFileDialogFolderPicker)
With myFolder
.Title = "Choose Folder"
.AllowMultiSelect = False
If .Show <> -1 Then
Exit Sub
End If
FolderSelected = .SelectedItems(1)
End With


Worksheets("Run").Range("C9").Value = FolderSelected


End Sub


Sub CreateGroups()


If Sheets("Run").Range("C9").Value = "" Then
    MsgBox "Please browse to the top level folder before running macro."
    Exit Sub
End If


Application.DisplayAlerts = False
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual


Dim thisWb, newWb As Workbook
Dim dRow, dEnd, fRow, groupCnt As Long
Dim path, newName As String


groupCnt = 0
dRow = 2
fRow = 2
dEnd = Sheets(2).UsedRange.Rows.Count


'Sort data by group
On Error Resume Next
Sheets(2).Range("A2:Z" & dEnd).Sort _
    Key1:=Sheets(2).Range("A2"), Order1:=xlAscending, _
    Key2:=Sheets(2).Range("C2"), Order2:=xlAscending, Header:=xlNo
    
Set thisWb = ActiveWorkbook


'Read through data, when Group changes, create folder and spreadsheet
Do While dRow <= dEnd
    
    If dRow <> 2 And thisWb.Sheets(2).Cells(dRow, 1).Value <> thisWb.Sheets(2).Cells(dRow - 1, 1).Value Then
        
        'Create folder
        path = thisWb.Sheets("Run").Range("C9").Value & "\" & thisWb.Sheets(2).Cells(fRow, 1).Value
        
        If Len(Dir(path, vbDirectory)) = 0 Then
            MkDir path
        End If
        
        'Create new file
        Set newWb = Workbooks.Add
        newName = path & "\" & thisWb.Sheets(2).Cells(fRow, 1).Value & ".xlsx"


        'Delete File
        On Error Resume Next
        Kill newName


        'Copy headings to Sheet1
        thisWb.Sheets(2).Range("C1:H1").Copy
        With newWb.Sheets(1).Range("A1")
            .Cells(1).PasteSpecial xlPasteColumnWidths
            .Cells(1).PasteSpecial xlPasteValues
            .Cells(1).PasteSpecial xlPasteFormats
            Application.CutCopyMode = False
        End With


        'Copy values to Sheet1
        thisWb.Sheets(2).Range("C" & fRow & ":H" & dRow - 1).Copy
        With newWb.Sheets(1).Range("A2")
            .Cells(1).PasteSpecial xlPasteColumnWidths
            .Cells(1).PasteSpecial xlPasteValues
            .Cells(1).PasteSpecial xlPasteFormats
            Application.CutCopyMode = False
        End With
    
        newWb.Sheets(1).Range("A1").Select


        With newWb
            .SaveAs newName
        End With
        
        newWb.Close
        
        groupCnt = groupCnt + 1
        fRow = dRow
    End If
    
    dRow = dRow + 1
Loop


Application.DisplayAlerts = True
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic


MsgBox groupCnt & " groups were processed."


End Sub
 
Upvote 0

Forum statistics

Threads
1,215,234
Messages
6,123,773
Members
449,123
Latest member
StorageQueen24

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