Sub Add_WS_for_Uniques()
Dim rngUniques As Range, cell As Range, rngSrc As Range, rngDst As Range, ccSrc As Range, ccDst As Range
Dim fmlaSrc As Range, fmlaDst As Range
Dim Lastrow As Long, Firstrow As Long
Dim ws As Worksheet, HCCosts As Worksheet, StaticData As Worksheet
Set HCCosts = Worksheets("2012HC")
Set StaticData = Worksheets("Static Data")
Application.ScreenUpdating = False
Lastrow = HCCosts.Range("F" & Rows.Count).End(xlUp).Row
HCCosts.Range("F1:F" & Lastrow).AdvancedFilter Action:=xlFilterInPlace, Unique:=True
Set rngUniques = HCCosts.Range("F2:F" & Lastrow).SpecialCells(xlCellTypeVisible)
HCCosts.ShowAllData
On Error Resume Next
For Each cell In rngUniques
If cell.Value <> "" Then
If Len(Sheets(cell.Value).Name) = 0 Then
Sheets.Add(After:=Sheets(Sheets.Count)).Name = cell.Value & " Natives"
Range("a1").Formula = "Pillar-Ledger"
Range("h1").Formula = "LU Col"
Range("i1").Formula = "Total P&L"
Range("a2").Value = cell.Value
End If
End If
Next cell
On Error GoTo 0
For Each ws In ActiveWorkbook.Sheets '(Array("",""))
If ws.Name Like "*Natives" Then
'copy Static Data
Lastrow = StaticData.Cells(Rows.Count, "C").End(xlUp).Row
Set rngSrc = StaticData.Range("C1:H" & Lastrow)
'paste Static Data
Set rngDst = ws.Range("B" & Rows.Count).End(xlUp).Offset(0, 0)
rngSrc.SpecialCells(xlCellTypeVisible).Copy
rngDst.PasteSpecial Paste:=xlPasteValues
rngDst.PasteSpecial Paste:=xlPasteFormats
Application.CutCopyMode = False
'copy LU Value
Lastrow = ws.Range("B" & Rows.Count).End(xlUp).Row
Set ccSrc = ws.Range("A2")
'paste LU Value
Set ccDst = ws.Range("A2:A" & Lastrow)
ccSrc.SpecialCells(xlCellTypeVisible).Copy
ccDst.PasteSpecial Paste:=xlPasteValues
ccDst.PasteSpecial Paste:=xlPasteFormats
Application.CutCopyMode = False
'Input LU Col and Total P&L Formulas
ws.Range("h2").Formula = "=IF(ISERROR(MATCH(F2,INDIRECT(""'""&E2&""'!E1:Z1""),0)),0," & _
"(MATCH(F2,INDIRECT(""'""&E2&""'!E1:Z1""),0)))"
ws.Range("i2").Formula = "=IF(ISNA(SUMIF('2012HC'!F:F,$A2,INDEX('2012HC'!$A:$Z," & _
",MATCH($F2,'2012HC'!$A$1:$Z$1,0)))),0,SUMIF('2012HC'!F:F,$A2," & _
"INDEX('2012HC'!$A:$Z,,MATCH($F2,'2012HC'!$A$1:$Z$1,0))))"
'copy Formulas
Set fmlaSrc = ws.Range("h2:i2")
'paste Formulas
Set fmlaDst = ws.Range("h2:i" & Lastrow)
fmlaSrc.SpecialCells(xlCellTypeVisible).Copy
fmlaDst.PasteSpecial Paste:=xlAll
Application.CutCopyMode = False
'Autofit columns
With ws.Columns("A:K")
.AutoFit
.ColumnWidth = .ColumnWidth + 2
End With
'ws.Outline.ShowLevels RowLevels:=1, ColumnLevels:=2
End If
Next ws
Application.ScreenUpdating = True
End Sub