VBA Macro

motopdx

New Member
Joined
Feb 24, 2020
Messages
13
Office Version
  1. 365
Platform
  1. Windows
I'm taking a data set (Sheet "Details") and separating data from that set based on a few drivers (2 different columns that represent the branch) and creating a new sheet within the same workbook. I have around a 100 locations that all need their own data tabs. I've provided an example for 2 locations which works but in order to have the macro work properly I have to repeat this macro and change the branch number. Is there a way to simplify this macro to break out the branch without having to write code for each location? Thank you for the help in advance.


' Branch 8
Sheets("Detail").Select
ActiveSheet.Range("$A$1").AutoFilter Field:=14, Criteria1:="8"
Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Sheets.Add After:=ActiveSheet
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
ActiveSheet.Name = "8"
'Copy next segment
Sheets("Detail").Select
ActiveSheet.ShowAllData
ActiveSheet.Range("$A$1").AutoFilter Field:=19, Criteria1:="8"
ActiveSheet.Range("$A$1").AutoFilter Field:=14, Criteria1:="<>8"
Range("A2:W2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy

Sheets("8").Select
Range("A1").Select
LastRow = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row + 1
Range("A" & LastRow).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("A1").Select

' Branch 28
Sheets("Detail").Select
ActiveSheet.ShowAllData
ActiveSheet.Range("$A$1").AutoFilter Field:=14, Criteria1:="28"
Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Sheets.Add After:=ActiveSheet
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
ActiveSheet.Name = "28"
'Copy next segment
Sheets("Detail").Select
ActiveSheet.ShowAllData
ActiveSheet.Range("$A$1").AutoFilter Field:=19, Criteria1:="28"
ActiveSheet.Range("$A$1").AutoFilter Field:=14, Criteria1:="<>28"
Range("A2:W2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy

Sheets("28").Select
Range("A1").Select
LastRow = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row + 1
Range("A" & LastRow).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("A1").Select

1594228028172.png
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
How about
VBA Code:
Sub motopdx()
   Dim Ws As Worksheet
   Dim Ary As Variant
   Dim i As Long
   
   Ary = Array(8, 28, 38)
   
   With Sheets("Detail")
      For i = 0 To UBound(Ary)
         Set Ws = Sheets.Add(, Sheets(Sheets.Count))
         Ws.Name = Ary(i)
         .Range("A1").AutoFilter 14, Ary(i)
         .AutoFilter.Range.EntireRow.Copy Ws.Range("A1")
         .Range("A1").AutoFilter 14, "<>" & Ary(i)
         .Range("A1").AutoFilter 19, Ary(i)
         .AutoFilter.Range.EntireRow.Copy Ws.Range("A" & Rows.Count).End(xlUp).Offset(1)
         .AutoFilterMode = False
      Next i
   End With
End Sub
 
Upvote 0
This is great and worked perfectly. Thank you very much for the quick reply and help.
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,590
Messages
6,120,423
Members
448,961
Latest member
nzskater

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