Hi, I'm trying to figure something out with a macro that I'm working on. I'm not very experienced with macros but I'm figuring it out as I go, and what I want to accomplish is working EXCEPT when the source table doesn't have data for a particular filter. Here are the details (with fictional data):
I have a master template with tabs for Apples, Oranges, Peaches, Grapes, and Bananas. Each year I will have data from 10 different farms, and I will be saving new Excel files from the template for each farm for each year. To do this I will copy the raw data from Farm 1 into a tab in the template (“Source Data”), run the macro to filter and copy and paste by each fruit into the respective tab. Then save as “Farm 1 – 2019.” I will do the same for each farm.
The problem is that not all farms have all 5 fruits. When the macro runs the filter and there aren’t any results in the table, it ends up copying ALL the data in the table. For example, if Farm 2 does not have Oranges, when the macro gets to filtering by “Oranges,” it will copy everything and paste it into the “Orange Data Tab.”
(For reference, the table headers are in Row 4.)
I have a master template with tabs for Apples, Oranges, Peaches, Grapes, and Bananas. Each year I will have data from 10 different farms, and I will be saving new Excel files from the template for each farm for each year. To do this I will copy the raw data from Farm 1 into a tab in the template (“Source Data”), run the macro to filter and copy and paste by each fruit into the respective tab. Then save as “Farm 1 – 2019.” I will do the same for each farm.
The problem is that not all farms have all 5 fruits. When the macro runs the filter and there aren’t any results in the table, it ends up copying ALL the data in the table. For example, if Farm 2 does not have Oranges, when the macro gets to filtering by “Oranges,” it will copy everything and paste it into the “Orange Data Tab.”
(For reference, the table headers are in Row 4.)
VBA Code:
Sheets("Source Data").Select
Selection.End(xlToLeft).Select
ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=1, Criteria1:= _
"Apples"
Range("B5:F5").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Sheets("Apple Data Tab").Select
Range("A8").Select
ActiveSheet.Paste
Sheets("Source Data").Select
Range("G5").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Apple Data Tab").Select
Range("G8").Select
ActiveSheet.Paste
Sheets("Source Data").Select
Range("H5:I5").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Apple Data Tab").Select
Range("I8").Select
ActiveSheet.Paste
Sheets("Source Data").Select
Selection.End(xlToLeft).Select
ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=1, Criteria1:= _
"Oranges"
Range("B5:F5").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Sheets("Orange Data Tab").Select
Range("A8").Select
ActiveSheet.Paste
Sheets("Source Data").Select
Range("G5").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Orange Data Tab").Select
Range("G8").Select
ActiveSheet.Paste
Sheets("Source Data").Select
Range("H5:I5").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Orange Data Tab").Select
Range("I8").Select
ActiveSheet.Paste