Sub TransferData()
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Dim ldateto As Long, ldatefrom As Long, LastRowS As Long, LastRowC As Long, x As Long
Dim fVisRow As Long, sMon As String
Dim wsCost As Worksheet, wsSales As Worksheet, wbDest As Workbook
Set wbDest = ActiveWorkbook
Set wsCost = Workbooks("2018_Cost.xlsx").Sheets("Sheet2")
Set wsSales = Workbooks("2018_Sales.xlsx").Sheets("Sheet2")
LastRowS = wsSales.Cells(Rows.Count, "W").End(xlUp).Row
LastRowC = wsCost.Cells(Rows.Count, "B").End(xlUp).Row
With wsSales
ldatefrom = DateSerial(2017, 12, 1)
ldateto = DateSerial(2017, 12 + 1, 0)
.Range("A4:X" & LastRowS).AutoFilter Field:=23, Criteria1:=">=" & ldatefrom, Operator:=xlAnd, Criteria2:="<=" & ldateto
.Range("A4:X" & LastRowS).AutoFilter Field:=3, Criteria1:="<>other"
.Range("A4:X" & LastRowS).AutoFilter Field:=22, Criteria1:="CF"
If .Range("V1:V" & .Range("V" & .Rows.Count).End(xlUp).Row).SpecialCells(xlCellTypeVisible).Cells.Count > 4 Then
Intersect(.Rows("5:" & LastRowS), .Range("A:A,D:F,J:J,M:M")).SpecialCells(xlCellTypeVisible).Copy wbDest.Sheets("Tutorial-Sales Dec-17").Cells(wbDest.Sheets("Tutorial-Sales Dec-17").Rows.Count, "A").End(xlUp).Offset(1, 0)
End If
.Range("W4").AutoFilter
For x = 1 To 8
ldatefrom = DateSerial(2018, x, 1)
ldateto = DateSerial(2018, x + 1, 0)
.Range("A4:X" & LastRowS).AutoFilter Field:=23, Criteria1:=">=" & ldatefrom, Operator:=xlAnd, Criteria2:="<=" & ldateto
.Range("A4:X" & LastRowS).AutoFilter Field:=3, Criteria1:="<>other"
.Range("A4:X" & LastRowS).AutoFilter Field:=22, Criteria1:="CF"
If .Range("V1:V" & .Range("V" & .Rows.Count).End(xlUp).Row).SpecialCells(xlCellTypeVisible).Cells.Count > 4 Then
fVisRow = .Range("W5", .Cells(.Rows.Count, "W").End(xlUp)).SpecialCells(xlCellTypeVisible).Cells(1, 1).Row
sMon = Left(MonthName(Month(.Range("W" & fVisRow))), 3)
Intersect(.Rows("5:" & LastRowS), .Range("A:A,D:F,J:J,M:M")).SpecialCells(xlCellTypeVisible).Copy wbDest.Sheets("Tutorial-Sales " & sMon & "-18").Cells(wbDest.Sheets("Tutorial-Sales " & sMon & "-18").Rows.Count, "A").End(xlUp).Offset(1, 0)
End If
.Range("W4").AutoFilter
Next x
ldatefrom = DateSerial(2017, 12, 1)
ldateto = DateSerial(2017, 12 + 1, 0)
.Range("A4:X" & LastRowS).AutoFilter Field:=23, Criteria1:=">=" & ldatefrom, Operator:=xlAnd, Criteria2:="<=" & ldateto
.Range("A4:X" & LastRowS).AutoFilter Field:=22, Criteria1:="AF"
If .Range("V1:V" & .Range("V" & .Rows.Count).End(xlUp).Row).SpecialCells(xlCellTypeVisible).Cells.Count > 4 Then
Intersect(.Rows("5:" & LastRowS), .Range("A:A,D:F,J:J,M:M")).SpecialCells(xlCellTypeVisible).Copy wbDest.Sheets("Tutorial-Sales Jan-18").Cells(wbDest.Sheets("Tutorial-Sales Jan-18").Rows.Count, "A").End(xlUp).Offset(1, 0)
End If
.Range("W4").AutoFilter
For x = 1 To 8
ldatefrom = DateSerial(2018, x, 1)
ldateto = DateSerial(2018, x + 1, 0)
.Range("A4:X" & LastRowS).AutoFilter Field:=23, Criteria1:=">=" & ldatefrom, Operator:=xlAnd, Criteria2:="<=" & ldateto
.Range("A4:X" & LastRowS).AutoFilter Field:=22, Criteria1:="AF"
If .Range("V1:V" & .Range("V" & .Rows.Count).End(xlUp).Row).SpecialCells(xlCellTypeVisible).Cells.Count > 4 Then
fVisRow = .Range("W5", .Cells(.Rows.Count, "W").End(xlUp)).SpecialCells(xlCellTypeVisible).Cells(1, 1).Row
sMon = Left(MonthName(Month(.Range("W" & fVisRow)) + 1), 3)
Intersect(.Rows("5:" & LastRowS), .Range("A:A,D:F,J:J,M:M")).SpecialCells(xlCellTypeVisible).Copy wbDest.Sheets("Tutorial-Sales " & sMon & "-18").Cells(wbDest.Sheets("Tutorial-Sales " & sMon & "-18").Rows.Count, "A").End(xlUp).Offset(1, 0)
End If
.Range("W4").AutoFilter
Next x
End With
With wsCost
ldatefrom = DateSerial(2017, 12, 1)
ldateto = DateSerial(2017, 12 + 1, 0)
.Range("A4:U" & LastRowC).AutoFilter Field:=2, Criteria1:=">=" & ldatefrom, Operator:=xlAnd, Criteria2:="<=" & ldateto
.Range("A4:U" & LastRowC).AutoFilter Field:=1, Criteria1:="<>other", Operator:=xlAnd, Criteria2:="<>overhead"
.Range("A4:U" & LastRowC).AutoFilter Field:=19, Criteria1:="CF"
If .Range("S1:S" & .Range("S" & .Rows.Count).End(xlUp).Row).SpecialCells(xlCellTypeVisible).Cells.Count > 4 Then
Intersect(.Rows("5:" & LastRowS), .Range("A:A,D:F,J:J,M:M")).SpecialCells(xlCellTypeVisible).Copy wbDest.Sheets("Tutorial-Cost Dec-17").Cells(wbDest.Sheets("Tutorial-Cost Dec-17").Rows.Count, "A").End(xlUp).Offset(1, 0)
End If
.Range("B4").AutoFilter
For x = 1 To 8
ldatefrom = DateSerial(2018, x, 1)
ldateto = DateSerial(2018, x + 1, 0)
.Range("A4:U" & LastRowC).AutoFilter Field:=2, Criteria1:=">=" & ldatefrom, Operator:=xlAnd, Criteria2:="<=" & ldateto
.Range("A4:U" & LastRowC).AutoFilter Field:=1, Criteria1:="<>other", Operator:=xlAnd, Criteria2:="<>overhead"
.Range("A4:U" & LastRowC).AutoFilter Field:=19, Criteria1:="CF"
If .Range("S1:S" & .Range("S" & .Rows.Count).End(xlUp).Row).SpecialCells(xlCellTypeVisible).Cells.Count > 4 Then
fVisRow = .Range("B5", .Cells(.Rows.Count, "B").End(xlUp)).SpecialCells(xlCellTypeVisible).Cells(1, 1).Row
sMon = Left(MonthName(Month(.Range("B" & fVisRow))), 3)
Intersect(.Rows("5:" & LastRowS), .Range("A:A,D:F,J:J,M:M")).SpecialCells(xlCellTypeVisible).Copy wbDest.Sheets("Tutorial-Cost " & sMon & "-18").Cells(wbDest.Sheets("Tutorial-Cost " & sMon & "-18").Rows.Count, "A").End(xlUp).Offset(1, 0)
End If
.Range("B4").AutoFilter
Next x
ldatefrom = DateSerial(2017, 12, 1)
ldateto = DateSerial(2017, 12 + 1, 0)
.Range("A4:U" & LastRowC).AutoFilter Field:=2, Criteria1:=">=" & ldatefrom, Operator:=xlAnd, Criteria2:="<=" & ldateto
.Range("A4:U" & LastRowC).AutoFilter Field:=1, Criteria1:="<>other", Operator:=xlAnd, Criteria2:="<>overhead"
.Range("A4:U" & LastRowC).AutoFilter Field:=19, Criteria1:="AF"
If .Range("S1:S" & .Range("S" & .Rows.Count).End(xlUp).Row).SpecialCells(xlCellTypeVisible).Cells.Count > 4 Then
Intersect(.Rows("5:" & LastRowS), .Range("A:A,D:F,J:J,M:M")).SpecialCells(xlCellTypeVisible).Copy wbDest.Sheets("Tutorial-Cost Jan-18").Cells(wbDest.Sheets("Tutorial-Cost Jan-18").Rows.Count, "A").End(xlUp).Offset(1, 0)
End If
.Range("B4").AutoFilter
For x = 1 To 8
ldatefrom = DateSerial(2018, x, 1)
ldateto = DateSerial(2018, x + 1, 0)
.Range("A4:U" & LastRowC).AutoFilter Field:=2, Criteria1:=">=" & ldatefrom, Operator:=xlAnd, Criteria2:="<=" & ldateto
.Range("A4:U" & LastRowC).AutoFilter Field:=1, Criteria1:="<>other", Operator:=xlAnd, Criteria2:="<>overhead"
.Range("A4:U" & LastRowC).AutoFilter Field:=19, Criteria1:="AF"
If .Range("S1:S" & .Range("S" & .Rows.Count).End(xlUp).Row).SpecialCells(xlCellTypeVisible).Cells.Count > 4 Then
fVisRow = .Range("B5", .Cells(.Rows.Count, "B").End(xlUp)).SpecialCells(xlCellTypeVisible).Cells(1, 1).Row
sMon = Left(MonthName(Month(.Range("B" & fVisRow)) + 1), 3)
Intersect(.Rows("5:" & LastRowS), .Range("A:A,D:F,J:J,M:M")).SpecialCells(xlCellTypeVisible).Copy wbDest.Sheets("Tutorial-Cost " & sMon & "-18").Cells(wbDest.Sheets("Tutorial-Cost " & sMon & "-18").Rows.Count, "A").End(xlUp).Offset(1, 0)
End If
.Range("B4").AutoFilter
Next x
End With
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub