Sub test()
Dim d(1) As Date, r As Range, f$, temp, ws As Worksheet, wb As Workbook, e, n&
Application.ScreenUpdating = False
d(0) = DateSerial(Year(Date), Month(Date) - 1, 1)
d(1) = WorksheetFunction.EoMonth(d(0), 0)
Set ws = Sheets.Add
For Each e In Array(Array("Cash Flow", "B11:G11"), Array("Pending", "A1:F1"))
ws.UsedRange.Clear
With ThisWorkbook.Sheets(e(0))
Set r = .[m1:m2]: temp = r.Value
With .Range(e(1)).Resize(.Cells(Rows.Count, Range(e(1)).Column).End(xlUp).Row - Range(e(1)).Row + 1)
.Rows(1).Copy ws.[a1]
f = .Range("a2").Address(0, 0)
r(2).Formula = Replace("=and(#>=" & CLng(d(0)) & ",#<=" & CLng(d(1)) & ")", "#", f)
.AdvancedFilter 2, r, ws.[a1].CurrentRegion
r.Value = temp
End With
End With
If ws.[a1].CurrentRegion.Rows.Count > 1 Then
n = n + 1
If wb Is Nothing Then Set wb = Workbooks.Add
With wb
If .Sheets.Count < n Then .Sheets.Add , .Sheets(n - 1)
.Sheets(n).Name = "Sheet" & n
ws.Cells.Copy .Sheets(n).[a1]
.Sheets(n).Columns.AutoFit
End With
End If
Next
If Not wb Is Nothing Then
f = Replace(CreateObject("Scripting.FileSystemObject").GetBaseName(ThisWorkbook.Name), "Cash Flow ", "")
Application.DisplayAlerts = False
wb.SaveAs ThisWorkbook.Path & "\" & f & "_" & Format$(d(0), "mmm"), 51
wb.Close
End If
Application.DisplayAlerts = False
ws.Delete
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub