In essence we want to create a VBA code that automatically creates Workbooks for each type of store (a column/variable in our dataset).
As an example, we have one source workbook with the following table:
Store Seller Item Price
A | Youtube | Banana | 5,00
B | Youtube | Apple | 6,00
A | Google | Apple | 7,00
C | Google | Pear | 5,00
B | Amazon | Citron | 4,50
What we want to achieve with the VBA code for the table above is three separate workbooks for Type of store A, B and C. The workbook needs to have the name of the Type of store. So it would look like:
~ A.xls ~
Store Seller Item Price
A | Youtube | Banana | 5,00
A | Google | Apple | 7,00
~ B.xls ~
Store Seller Item Price
B | Youtube | Apple | 6,00
B | Amazon | Citron | 4,50
~ C.xls ~
Store Seller Item Price
C | Google | Pear | 5,00
I had a go with a very crude way of doing it (see below) but there are a few things missing:
- An efficient loop
- The
Windows(“Map4”).Activate
messes up a potential loop - And a way of naming the file according to the
‘Type of store’
Sub Macro1()
ActiveSheet.Range("$A$1:$A$8" & "$C$1:$C$8").AutoFilter Field:=2,
Criteria1:="aa"
Workbooks.Add
Windows("Test_split file.xlsm").Activate
Range("A1:C8").Select
Selection.Copy
Windows("Map4").Activate
ActiveSheet.Paste
Application.CutCopyMode = False
ActiveWorkbook.SaveAs Filename:="C:\Users\bjprent\Documents\aa.xlsx", _
FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
ActiveWindow.Close
ActiveSheet.Range("$A$1:$C$8").AutoFilter Field:=2, Criteria1:="bb"
Workbooks.Add
Windows("Test_split file.xlsx").Activate
Range("A1:C8").Select
Selection.Copy
Windows("Map4").Activate
ActiveSheet.Paste
Application.CutCopyMode = False
ActiveWorkbook.SaveAs Filename:="C:\Users\bjprent\Documents\bb.xlsx", _
FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
ActiveWindow.Close
End Sub
Thanks in advance for any help! :)