VBA to filter data and copy various sheets to new workbook

davey4444

Board Regular
Joined
Nov 16, 2010
Messages
97
Hello,

I have a workbook which has a list of managers and customers and I would like to create a new workbook for each manager which only lists their particular customers. I've seen some VBA which can do this however the additional element is that I would like the new workbook to include some other sheets from the original file too.
Additionally I would then like to save the newly-created file with the name format of Region-Area-Manager if possible.
Below is an example of how the data is laid out in the main template file so, for example, the first newly-created file would show just the data for "Sales 1", togther with various other sheets from that file.


ABCDEFG
1PersonRegionAreaCustomer NameProd AProd BProd C
2Sales 1ScotlandOther ScotlandCustomer LIY123
3Sales 1ScotlandOther ScotlandCustomer CLN123
4Sales 1ScotlandOther ScotlandCustomer IHA123
5Sales 1ScotlandOther ScotlandCustomer JKI123
6Sales 10EnglandWestCustomer FDM0.90.515
7Sales 10EnglandWestCustomer YBI0.90.515
8Sales 10EnglandWestCustomer XKB0.90.515
9Sales 10EnglandWestCustomer QWY0.90.515
10Sales 11WalesOther WalesCustomer YSV0.90.515
11Sales 11WalesOther WalesCustomer EUZ0.90.515
12Sales 11WalesOther WalesCustomer ARL0.90.515
13Sales 11WalesOther WalesCustomer ALZ0.90.515
14Sales 12WalesGlamorganCustomer ENN0.90.515
15Sales 12WalesGlamorganCustomer IEH0.90.515
16Sales 12WalesGlamorganCustomer EHJ0.90.515
17Sales 12WalesGlamorganCustomer OBY0.90.515
18Sales 2ScotlandDundeeCustomer AVL123
19Sales 2ScotlandDundeeCustomer OZQ123

<tbody>
</tbody>
Template 1
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
How about
Code:
Sub davey4444()
   Dim Ary As Variant, Ky As Variant
   Dim Dic As Object
   Dim Cl As Range
   
   Set Dic = CreateObject("scripting.dictionary")
   Ary = Array("Template 1", "Data", "Test")
   With Sheets("Template 1")
      For Each Cl In .Range("A2", .Range("A" & Rows.Count).End(xlUp))
         Dic.item(Cl.Value) = Cl.Offset(, 1).Value & "-" & Cl.Offset(, 2).Value & "-" & Cl.Value
      Next Cl
   End With
   For Each Ky In Dic.keys
      Sheets(Ary).Copy
      With ActiveWorkbook.Sheets("Template 1")
         .Range("a1").AutoFilter 1, "<>" & Ky
         .AutoFilter.Range.Offset(1).EntireRow.Delete
         .AutoFilterMode = False
      End With
      ActiveWorkbook.SaveAs Dic(Ky), 51
   Next Ky
End Sub
 
Upvote 0
Thanks for the quick reply. This code managed to create a new book for each person in the expected format however only first one created (Sales 1) showed any data in it with all of the other workbooks just showing the first row.
 
Upvote 0
Ok, how about
Code:
Sub davey4444()
   Dim Ary As Variant, Ky As Variant
   Dim Dic As Object
   Dim Cl As Range
   
   Application.ScreenUpdating = False
   Set Dic = CreateObject("scripting.dictionary")
   Ary = Array("Template 1", "Data", "Test")
   With Sheets("Template 1")
      For Each Cl In .Range("A2", .Range("A" & Rows.Count).End(xlUp))
         Dic.item(Cl.Value) = Cl.Offset(, 1).Value & "-" & Cl.Offset(, 2).Value & "-" & Cl.Value
      Next Cl
   End With
   For Each Ky In Dic.keys
      Sheets(Ary).Copy
      With ActiveWorkbook.Sheets("Template 1")
         .Range("a1").AutoFilter 1, "<>" & Ky
         .AutoFilter.Range.Offset(1).EntireRow.Delete
         .AutoFilterMode = False
      End With
      ActiveWorkbook.SaveAs "C:\Mrexcel\Fluff\" & Dic(Ky), 51
      ActiveWorkbook.Close
   Next Ky
End Sub
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,214,651
Messages
6,120,738
Members
448,988
Latest member
BB_Unlv

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