Sub CompanyCode()
'Assumes company code begins in A2. Run code with the raw data sheet active
Dim DataSht As Worksheet, R As Range, CompanyIDs As Range, ID As Range
Set DataSht = ActiveSheet
Set R = Range("A1").CurrentRegion
Application.ScreenUpdating = False
Set CompanyIDs = Cells(R.Rows.Count + 3, R.Columns.Count + 3)
CompanyIDs.EntireColumn.Insert
R.Columns(1).AdvancedFilter xlFilterCopy, copytorange:=CompanyIDs, unique:=True
Set CompanyIDs = Range(CompanyIDs(2), CompanyIDs.End(xlDown))
For Each ID In CompanyIDs
R.AutoFilter field:=1, Criteria1:=ID
If R.SpecialCells(xlCellTypeVisible).Rows.Count > 1 Or R.SpecialCells(xlCellTypeVisible).Areas.Count > 1 Then
On Error Resume Next
Application.DisplayAlerts = False
Sheets(ID.Value).Delete
Sheets.Add after:=DataSht
ActiveSheet.Name = ID
R.Copy Sheets(ID.Value).Range("A1")
Sheets(ID.Value).Range("A1").CurrentRegion.EntireColumn.AutoFit
End If
Next ID
CompanyIDs.EntireColumn.Delete
With DataSht
.AutoFilterMode = False
.Activate
End With
With Application
.DisplayAlerts = True
.ScreenUpdating = True
End With
End Sub