VBA to filter and save as new workbook

thedeadzeds

Active Member
Joined
Aug 16, 2011
Messages
442
Office Version
  1. 365
Platform
  1. Windows
Hi Guys,

I have the following worksheet. I would like to be able to do the following if possible:

Filter by every Name (column a) and then save columns A to Z in as a new workbook in a new folder. So for the below example, in the folder titled 'Names', there would be 3 workbooks, 1 for Dave with only Daves data in it, 1 for Jim and so on.

Many thanks


NameDateProductType12345678910111213141516171819
DaveOct-20Product 1CoreProduct 1£1.00£2.0094.2%295£295.005.8%18£36.00313£331.0080.0%250£250.4020.0%63£125.20313£375.60Normal£650.00
DaveOct-20Product 2CoreProduct 2£1.00£2.0091.4%169£169.008.6%16£32.00185£201.0080.0%148£148.0020.0%37£74.00185£222.0080%£727.20
DaveOct-20Product 3CoreProduct 3£1.00£2.0088.5%77£77.0011.5%10£20.0087£97.0080.0%70£69.6020.0%17£34.8087£104.40
DaveOct-20Product 4CoreProduct 4£1.00£2.00100.0%1£1.000.0%0£0.001£1.0080.0%1£0.8020.0%0£0.401£1.20
DaveOct-20Product 5CoreProduct 5£1.00£2.00100.0%4£4.000.0%0£0.004£4.0080.0%3£3.2020.0%1£1.604£4.80
DaveOct-20Product 6CoreProduct 6£1.00£2.00100.0%2£2.000.0%0£0.002£2.0080.0%2£1.6020.0%0£0.802£2.40
DaveOct-20Product 7CoreProduct 7£1.00£2.000.0%0£0.000.0%0£0.000£0.000.0%0£0.000.0%0£0.000£0.00
DaveOct-20Product 8UpsellProduct 8£1.00£2.00100.0%1£1.000.0%0£0.001£1.0080.0%1£0.8020.0%0£0.401£1.20
DaveOct-20Product 9UpsellProduct 9£1.00£2.00100.0%4£4.000.0%0£0.004£4.0080.0%3£3.2020.0%1£1.604£4.80
DaveOct-20Product 10UpsellProduct 10£1.00£2.000.0%0£0.000.0%0£0.000£0.000.0%0£0.000.0%0£0.000£0.00
DaveOct-20Product 11UpsellProduct 11£1.00£2.00100.0%1£1.000.0%0£0.001£1.0080.0%1£0.8020.0%0£0.401£1.20
DaveOct-20Product 12UpsellProduct 12£1.00£2.00100.0%5£5.000.0%0£0.005£5.0080.0%4£4.0020.0%1£2.005£6.00
DaveOct-20Product 13UpsellProduct 13£1.00£2.00100.0%3£3.000.0%0£0.003£3.0080.0%2£2.4020.0%1£1.203£3.60
DaveOct-20Product 14UpsellProduct 14£1.00£2.000.0%0£0.000.0%0£0.000£0.000.0%0£0.000.0%0£0.000£0.00
DaveOct-20Product 15UpsellProduct 15£1.00£2.000.0%0£0.000.0%0£0.000£0.000.0%0£0.000.0%0£0.000£0.00
DaveOct-20Product 16UpsellProduct 16£1.00£2.000.0%0£0.000.0%0£0.000£0.000.0%0£0.000.0%0£0.000£0.00
DaveOct-20Product 17UpsellProduct 17£1.00£2.000.0%0£0.000.0%0£0.000£0.000.0%0£0.000.0%0£0.000£0.00
DaveOct-20Product 18UpsellProduct 18£1.00£2.000.0%0£0.000.0%0£0.000£0.000.0%0£0.000.0%0£0.000£0.00
JimOct-20Product 1CoreProduct 1£1.00£2.0099.0%102£102.001.0%1£2.00103£104.0080.0%82£82.4020.0%21£41.20103£123.60Normal£222.00
JimOct-20Product 2CoreProduct 2£1.00£2.0098.4%60£60.001.6%1£2.0061£62.0080.0%49£48.8020.0%12£24.4061£73.2080%£264.00
JimOct-20Product 3CoreProduct 3£1.00£2.00100.0%55£55.000.0%0£0.0055£55.0080.0%44£44.0020.0%11£22.0055£66.00
JimOct-20Product 4CoreProduct 4£1.00£2.000.0%0£0.000.0%0£0.000£0.000.0%0£0.000.0%0£0.000£0.00
JimOct-20Product 5CoreProduct 5£1.00£2.000.0%0£0.000.0%0£0.000£0.000.0%0£0.000.0%0£0.000£0.00
JimOct-20Product 6CoreProduct 6£1.00£2.000.0%0£0.000.0%0£0.000£0.000.0%0£0.000.0%0£0.000£0.00
JimOct-20Product 7CoreProduct 7£1.00£2.000.0%0£0.000.0%0£0.000£0.000.0%0£0.000.0%0£0.000£0.00
JimOct-20Product 8UpsellProduct 8£1.00£2.000.0%0£0.000.0%0£0.000£0.000.0%0£0.000.0%0£0.000£0.00
JimOct-20Product 9UpsellProduct 9£1.00£2.000.0%0£0.000.0%0£0.000£0.000.0%0£0.000.0%0£0.000£0.00
JimOct-20Product 10UpsellProduct 10£1.00£2.00100.0%1£1.000.0%0£0.001£1.0080.0%1£0.8020.0%0£0.401£1.20
JimOct-20Product 11UpsellProduct 11£1.00£2.000.0%0£0.000.0%0£0.000£0.000.0%0£0.000.0%0£0.000£0.00
JimOct-20Product 12UpsellProduct 12£1.00£2.000.0%0£0.000.0%0£0.000£0.000.0%0£0.000.0%0£0.000£0.00
JimOct-20Product 13UpsellProduct 13£1.00£2.000.0%0£0.000.0%0£0.000£0.000.0%0£0.000.0%0£0.000£0.00
JimOct-20Product 14UpsellProduct 14£1.00£2.000.0%0£0.000.0%0£0.000£0.000.0%0£0.000.0%0£0.000£0.00
JimOct-20Product 15UpsellProduct 15£1.00£2.000.0%0£0.000.0%0£0.000£0.000.0%0£0.000.0%0£0.000£0.00
JimOct-20Product 16UpsellProduct 16£1.00£2.000.0%0£0.000.0%0£0.000£0.000.0%0£0.000.0%0£0.000£0.00
JimOct-20Product 17UpsellProduct 17£1.00£2.000.0%0£0.000.0%0£0.000£0.000.0%0£0.000.0%0£0.000£0.00
JimOct-20Product 18UpsellProduct 18£1.00£2.000.0%0£0.000.0%0£0.000£0.000.0%0£0.000.0%0£0.000£0.00
SteveOct-20Product 1CoreProduct 1£1.00£2.000.0%0£0.000.0%0£0.000£0.000.0%0£0.000.0%0£0.000£0.00Normal£0.00
SteveOct-20Product 2CoreProduct 2£1.00£2.000.0%0£0.000.0%0£0.000£0.000.0%0£0.000.0%0£0.000£0.0080%£0.00
SteveOct-20Product 3CoreProduct 3£1.00£2.000.0%0£0.000.0%0£0.000£0.000.0%0£0.000.0%0£0.000£0.00
SteveOct-20Product 4CoreProduct 4£1.00£2.000.0%0£0.000.0%0£0.000£0.000.0%0£0.000.0%0£0.000£0.00
SteveOct-20Product 5CoreProduct 5£1.00£2.000.0%0£0.000.0%0£0.000£0.000.0%0£0.000.0%0£0.000£0.00
SteveOct-20Product 6CoreProduct 6£1.00£2.000.0%0£0.000.0%0£0.000£0.000.0%0£0.000.0%0£0.000£0.00
SteveOct-20Product 7CoreProduct 7£1.00£2.000.0%0£0.000.0%0£0.000£0.000.0%0£0.000.0%0£0.000£0.00
SteveOct-20Product 8UpsellProduct 8£1.00£2.000.0%0£0.000.0%0£0.000£0.000.0%0£0.000.0%0£0.000£0.00
SteveOct-20Product 9UpsellProduct 9£1.00£2.000.0%0£0.000.0%0£0.000£0.000.0%0£0.000.0%0£0.000£0.00
SteveOct-20Product 10UpsellProduct 10£1.00£2.000.0%0£0.000.0%0£0.000£0.000.0%0£0.000.0%0£0.000£0.00
SteveOct-20Product 11UpsellProduct 11£1.00£2.000.0%0£0.000.0%0£0.000£0.000.0%0£0.000.0%0£0.000£0.00
SteveOct-20Product 12UpsellProduct 12£1.00£2.000.0%0£0.000.0%0£0.000£0.000.0%0£0.000.0%0£0.000£0.00
SteveOct-20Product 13UpsellProduct 13£1.00£2.000.0%0£0.000.0%0£0.000£0.000.0%0£0.000.0%0£0.000£0.00
SteveOct-20Product 14UpsellProduct 14£1.00£2.000.0%0£0.000.0%0£0.000£0.000.0%0£0.000.0%0£0.000£0.00
SteveOct-20Product 15UpsellProduct 15£1.00£2.000.0%0£0.000.0%0£0.000£0.000.0%0£0.000.0%0£0.000£0.00
SteveOct-20Product 16UpsellProduct 16£1.00£2.000.0%0£0.000.0%0£0.000£0.000.0%0£0.000.0%0£0.000£0.00
SteveOct-20Product 17UpsellProduct 17£1.00£2.000.0%0£0.000.0%0£0.000£0.000.0%0£0.000.0%0£0.000£0.00
SteveOct-20Product 18UpsellProduct 18£1.00£2.000.0%0£0.000.0%0£0.000£0.000.0%0£0.000.0%0£0.000£0.00
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Hi,​
according to Excel basics it should be easily achieved if only all the columns have an unique header even any blank column …​
Anyway, is column A always sorted before to run the VBA procedure ? (As if yes there is another way …)
 
Upvote 0
Hi,​
according to Excel basics it should be easily achieved if only all the columns have an unique header even any blank column …​
Anyway, is column A always sorted before to run the VBA procedure ? (As if yes there is another way …)
Hi, yes Column A will awlays be sorted by Name
 
Upvote 0
According to your attachment a VBA demonstration as a beginner starter where the destination folder must already exist,​
the sort of column A is added for safety, to paste to the worksheet module :​
VBA Code:
Sub Demo1()
     Dim P$, Ws As Worksheet, L&, F&, N$
         P = ThisWorkbook.Path & "\Names\"
    With Application
        .DisplayAlerts = False
        .ScreenUpdating = False
         Set Ws = Workbooks.Add.Worksheets(1)
    With [A1].CurrentRegion.Resize(, 26).Rows
           .Item(1).Copy Ws.[A1]
           .Sort .Cells(1), 1, Header:=xlYes
            L = 1
        While Not IsEmpty(.Cells(L + 1, 1))
            Ws.UsedRange.Offset(1).Clear
            F = L + 1
            N = .Cells(F, 1).Text
            L = .Columns(1).Find(N, , , xlWhole, , 2).Row
           .Item(F & ":" & L).Copy Ws.[A2]
            Ws.Parent.SaveAs P & N, 51
        Wend
    End With
         Ws.Parent.Close False
        .Speech.Speak "Done !", True
        .ScreenUpdating = True
        .DisplayAlerts = True
    End With
End Sub
 
Upvote 0
According to your attachment a VBA demonstration as a beginner starter where the destination folder must already exist,​
the sort of column A is added for safety, to paste to the worksheet module :​
VBA Code:
Sub Demo1()
     Dim P$, Ws As Worksheet, L&, F&, N$
         P = ThisWorkbook.Path & "\Names\"
    With Application
        .DisplayAlerts = False
        .ScreenUpdating = False
         Set Ws = Workbooks.Add.Worksheets(1)
    With [A1].CurrentRegion.Resize(, 26).Rows
           .Item(1).Copy Ws.[A1]
           .Sort .Cells(1), 1, Header:=xlYes
            L = 1
        While Not IsEmpty(.Cells(L + 1, 1))
            Ws.UsedRange.Offset(1).Clear
            F = L + 1
            N = .Cells(F, 1).Text
            L = .Columns(1).Find(N, , , xlWhole, , 2).Row
           .Item(F & ":" & L).Copy Ws.[A2]
            Ws.Parent.SaveAs P & N, 51
        Wend
    End With
         Ws.Parent.Close False
        .Speech.Speak "Done !", True
        .ScreenUpdating = True
        .DisplayAlerts = True
    End With
End Sub
Thanks for this. Ive created the folder 'Names' in the same path as the workbook but nothing seems to happen. I can hear 'done' so its run through the code, but I cant see the new individual worksheets anywhere
 
Upvote 0
As this code works only with good enough readers so well follow the dark read direction of my previous post …​
If the code is located where it must be, as it works as expected on my side​
so you must investigate on VBE side in debug step-by-step mode hitting F8 key​
in order to follow the execution, check the variables contents and see what happens …​
 
Upvote 0

Forum statistics

Threads
1,214,649
Messages
6,120,733
Members
448,987
Latest member
marion_davis

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