Lalaarif, maybe like this
Sub Splitfiles()
Dim dic As Object, rng As Range, rng2 As Range, wks As Worksheet, mypath As String, mystring As String
Set dic = CreateObject("scripting.dictionary")
Set wks = Sheets("Data")
mypath = ThisWorkbook.Path & "\"
Application.ScreenUpdating = False
With wks
.Columns.Hidden = False
For nrow = 7 To .Cells(Rows.Count, "BB").End(xlUp).Row - 1
If (Not dic.exists(.Cells(nrow, "BB").Value)) Then
dic.Add .Cells(nrow, "BB").Value, .Cells(nrow, "BB").Value
Set rng = .Range("A6:BB" & .Cells(Rows.Count, 1).End(xlUp).Row)
rng.AutoFilter field:=54, Criteria1:=.Range("BB" & nrow).Value
rng.Copy
Workbooks.Add
ActiveSheet.Paste
Range("A1", "A5").EntireRow.Insert
lr = Range("A" & Rows.Count).End(xlUp).Row
Range("L1").FormulaR1C1 = "=sum(R6C:R20000C)"
Range("L5").FormulaR1C1 = "=subtotal(9,R7C:R20000C)"
Range("A5").FormulaR1C1 = "=R5C12"
Range("T5").FormulaR1C1 = "=R5C12"
Range("V5").FormulaR1C1 = "=R5C12"
Range("AV5").FormulaR1C1 = "=R5C12"
Range("AW5").FormulaR1C1 = "=R5C12"
Range("A1").Resize(3) = Application.Transpose(Array(.Range("A1"), .Range("A2"), .Range("BB" & nrow)))
ActiveSheet.Columns.AutoFit
Rows("6:6").AutoFilter
Columns("L:L").NumberFormat = "_(* #,##0_);[Red]_(* (#,##0);_(* ""-""??_);_(@_)"
Range("g7").Select
ActiveWindow.FreezePanes = True
Range("A2").NumberFormat = "mmm yy"
Range("a5").NumberFormat = "_(* #,##0_);[Red]_(* (#,##0);_(* ""-""??_);_(@_)"
ActiveWindow.Zoom = 85
ActiveSheet.Name = "YTD " & wks.Range("A1")
Set wks2 = Sheets("YTD " & wks.Range("A1"))
lr2 = wks2.Cells(Rows.Count, "S").End(xlUp).Row
mystring = ""
For x = 7 To lr2
r = InStr(1, mystring, wks2.Range("S" & x))
If r = 0 Then
mystring = mystring & "|" & wks2.Range("S" & x)
wks2.Range("A1", "BB" & lr).AutoFilter field:=19, Criteria1:=wks2.Cells(x, "S")
wks2.Range("A1", "BB" & lr).SpecialCells(xlCellTypeVisible).Copy
Sheets.Add after:=Sheets(Sheets.Count)
ActiveSheet.Paste
Range("L1").FormulaR1C1 = "=sum(R6C:R20000C)"
Range("L5").FormulaR1C1 = "=subtotal(9,R7C:R20000C)"
Range("A5").FormulaR1C1 = "=R5C12"
Range("T5").FormulaR1C1 = "=R5C12"
Range("V5").FormulaR1C1 = "=R5C12"
Range("AV5").FormulaR1C1 = "=R5C12"
Range("AW5").FormulaR1C1 = "=R5C12"
Range("A1").Resize(3) = Application.Transpose(Array(.Range("A1"), wks2.Range("S" & x), .Range("BB" & nrow)))
Columns.AutoFit
Rows("6:6").AutoFilter
Columns("L:L").NumberFormat = "_(* #,##0_);[Red]_(* (#,##0);_(* ""-""??_);_(@_)"
Range("g7").Select
ActiveWindow.FreezePanes = True
Range("A2").NumberFormat = "mmm yy"
Range("A5").NumberFormat = "_(* #,##0_);[Red]_(* (#,##0);_(* ""-""??_);_(@_)"
ActiveWindow.Zoom = 85
ActiveSheet.Name = "YTD " & Format(Range("A2"), "mmm yy") & " " & wks.Range("A1")
End If
Next
wks2.Range("A1", "BB" & lr2).AutoFilter field:=19
ActiveWorkbook.SaveAs Filename:=mypath & "Cost Dump " & wks.Range("A1") & " " & wks.Range("A2") & " " & .Range("BB" & nrow).Value & ".xlsx"
ActiveWorkbook.Close
End If
.AutoFilterMode = False
Next
End With
Application.CutCopyMode = False
Application.ScreenUpdating = True
MsgBox "File Save As Completed"
End Sub
Cheers
Leo
Bookmarks