Sub CreateWorkbooks()
Application.ScreenUpdating = False
Dim posWS As Worksheet, assWS As Worksheet, v As Variant, dic As Object, i As Long, WB As Workbook
Set WB = ThisWorkbook
Set posWS = WB.Sheets("Position")
Set assWS = WB.Sheets("Assignment")
Set dic = CreateObject("Scripting.Dictionary")
With posWS
v = .Range("E4", .Range("E" & .Rows.Count).End(xlUp)).Value
For i = LBound(v) To UBound(v)
If Not dic.exists(v(i, 1)) Then
dic.Add v(i, 1), Nothing
.Range("A1").CurrentRegion.AutoFilter 5, v(i, 1)
.AutoFilter.Range.Offset(1).Copy
Workbooks.Add
Range("A4").PasteSpecial
ActiveSheet.Name = "Position"
.Range("A1").AutoFilter
.Range("A1:Y3").Copy Range("A1")
Columns.AutoFit
Sheets.Add(After:=Sheets(Sheets.Count)).Name = "Assignment"
With assWS
.Range("A1").CurrentRegion.AutoFilter 5, v(i, 1)
.AutoFilter.Range.Copy
Range("A1").PasteSpecial
Sheets("Position").Range("P4", Sheets("Position").Range("P" & Rows.Count).End(xlUp)).Formula = "=COUNTIFS(Assignment!$H$2:$H$" & assWS.Range("P" & Rows.Count).End(xlUp).Row & ",$H4)"
.Range("A1").AutoFilter
Columns.AutoFit
End With
With ActiveWorkbook
Application.DisplayAlerts = False
.Sheets("Sheet2").Delete
.Sheets("Sheet3").Delete
.SaveAs Filename:="C:\Users\shell\Documents\" & v(i, 1), FileFormat:=51
.Close False
Application.DisplayAlerts = True
End With
End If
Next i
End With
Application.ScreenUpdating = True
End Sub