Try this.
- This will filter for each unique location in column L on the Input sheet and copy the filtered rows to its named worksheet and the AllBranches sheet.
- If a location doesn't have a named worksheet, it will copy the Template worksheet and rename the copy as the new location.
- Before running this code, first copy one of the empty location sheets and name it Template. You could hide the Template sheet if you want.
- The code doesn't use the location list in column U on the Input sheet
Sub Copy_Data_To_Tabs()
Dim LastRow As Long, rngUniqueLocs As Range, rngLoc As Range
With Sheets("Input")
LastRow = .Range("L:L").Find("*", , , , 1, 2).Row 'Last row of input data
If LastRow = 1 Then MsgBox "'Input' data sheet is empty. ", , "No Data": Exit Sub
Application.ScreenUpdating = False
'List of unique locations (column L) in the input data
.Range("L1:L" & LastRow).AdvancedFilter xlFilterInPlace, Unique:=True
Set rngUniqueLocs = .Range("L2:L" & LastRow).SpecialCells(xlCellTypeVisible)
If .FilterMode Then .ShowAllData
For Each rngLoc In rngUniqueLocs 'Loop through each unique location in the data
If Not Evaluate("ISREF('" & rngLoc.Value & "'!A1)") Then 'Test if the location has a worksheet
Sheets("Template").Copy After:=Sheets(Sheets.Count) 'Copy the template sheet
ActiveSheet.Name = rngLoc.Value 'Name new sheet
ActiveSheet.Visible = True
End If
'Filter on Loction
.Range("L1:L" & LastRow).AutoFilter 1, rngLoc.Value
'Copy filtered data
.Range("A2:M" & LastRow).Copy Sheets(rngLoc.Value).Cells(Rows.Count, 1).End(xlUp).Offset(1)
.Range("A2:M" & LastRow).Copy Sheets("AllBranches").Cells(Rows.Count, 1).End(xlUp).Offset(1)
Next rngLoc
.AutoFilterMode = False
.Select
Application.ScreenUpdating = True
End With
MsgBox "Data copied to each worksheet. ", , "Copy Complete"
End Sub
Bookmarks