Sub CopyCols()
Application.ScreenUpdating = False
Dim USDsh As Worksheet, EURsh As Worksheet, DATAsh As Worksheet, PCsh As Worksheet, RCsh As Worksheet
Set USDsh = Workbooks("Invoice_List_Purchase_2018.xlsx").Sheets("USD")
Set EURsh = Workbooks("Invoice_List_Purchase_2018.xlsx").Sheets("EUR")
Set DATAsh = Workbooks("Sales Report YTD.xlsx").Sheets("DATA")
Set PCsh = ThisWorkbook.Sheets("Purchase Costs")
Set RCsh = ThisWorkbook.Sheets("Revenue Costs")
PCsh.UsedRange.Offset(1, 0).ClearContents
RCsh.UsedRange.Offset(1, 0).ClearContents
Dim beginDate As String
Dim endDate As String
Dim lastRow As Long
Dim bottomA As Long, bottomB As Long, x As Long
x = 2
Dim WIP As Range
Dim ws As Worksheet
For Each ws In Workbooks("Invoice_List_Purchase_2018.xlsx").Sheets(Array("USD", "EUR"))
If ws.Name = "USD" Then
bottomA = ws.Range("A" & ws.Rows.Count).End(xlUp).Row
Intersect(ws.Rows("2:" & bottomA), ws.Range("A:A,D:E,G:G,H:H")).Copy PCsh.Cells(PCsh.Rows.Count, "A").End(xlUp).Offset(1, 0)
Intersect(ws.Rows("2:" & bottomA), ws.Range("L:L,N:N,S:S")).Copy PCsh.Cells(PCsh.Rows.Count, "G").End(xlUp).Offset(1, 0)
ElseIf ws.Name = "EUR" Then
bottomB = ws.Range("B" & ws.Rows.Count).End(xlUp).Row
Intersect(ws.Rows("2:" & bottomB), ws.Range("D:F")).Copy PCsh.Cells(PCsh.Rows.Count, "B").End(xlUp).Offset(1, 0)
Intersect(ws.Rows("2:" & bottomB), ws.Range("G:G")).Copy PCsh.Range("F" & PCsh.Range("E" & PCsh.Rows.Count).End(xlUp).Row + 1)
Intersect(ws.Rows("2:" & bottomB), ws.Range("K:K")).Copy PCsh.Range("G" & PCsh.Range("E" & PCsh.Rows.Count).End(xlUp).Row + 1)
Intersect(ws.Rows("2:" & bottomB), ws.Range("S:S")).Copy PCsh.Range("I" & PCsh.Range("E" & PCsh.Rows.Count).End(xlUp).Row + 1)
End If
Next ws
bottomA = DATAsh.Range("A" & DATAsh.Rows.Count).End(xlUp).Row
Intersect(DATAsh.Rows("2:" & bottomA), DATAsh.Range("A:C")).Copy RCsh.Cells(RCsh.Rows.Count, "B").End(xlUp).Offset(1, 0)
Intersect(DATAsh.Rows("2:" & bottomA), DATAsh.Range("I:I")).Copy RCsh.Cells(RCsh.Rows.Count, "E").End(xlUp).Offset(1, 0)
Intersect(DATAsh.Rows("2:" & bottomA), DATAsh.Range("L:L")).Copy RCsh.Cells(RCsh.Rows.Count, "G").End(xlUp).Offset(1, 0)
Intersect(DATAsh.Rows("2:" & bottomA), DATAsh.Range("P:P")).Copy RCsh.Cells(RCsh.Rows.Count, "I").End(xlUp).Offset(1, 0)
ReTry1:
beginDate = InputBox("Please enter the start date in format mm/dd/yyyy", "Beginning date", Format(Now(), "mm/dd/yyyy"))
If beginDate = "" Then
MsgBox ("You have not entered a date.")
PCsh.UsedRange.Offset(1, 0).ClearContents
RCsh.UsedRange.Offset(1, 0).ClearContents
Exit Sub
End If
If Format(beginDate, "mm/dd/yyyy") <> beginDate Then
MsgBox "Wrong date format. Please enter in format mm/dd/yyyy.": GoTo ReTry1
End If
ReTry2:
endDate = InputBox("Please enter the end date in format mm/dd/yyyy", "End date", Format(Now(), "mm/dd/yyyy"))
If endDate = "" Then
MsgBox ("You have not entered a date.")
PCsh.UsedRange.Offset(1, 0).ClearContents
RCsh.UsedRange.Offset(1, 0).ClearContents
Exit Sub
End If
If Format(endDate, "mm/dd/yyyy") <> endDate Then
MsgBox "Wrong date format. Please enter in format mm/dd/yyyy.": GoTo ReTry2
End If
PCsh.Activate
PCsh.Columns.AutoFit
lastRow = PCsh.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
PCsh.Range("A1:K" & lastRow).AutoFilter Field:=1, Criteria1:="<>Material"
If PCsh.Range("A2", Cells(PCsh.Rows.Count, "A").End(xlUp)).SpecialCells(xlCellTypeVisible).Cells(1, 1).Row > 1 Then
PCsh.Range("A2:K" & PCsh.Range("A" & PCsh.Rows.Count).End(xlUp).Row).SpecialCells(xlCellTypeVisible).EntireRow.Delete
End If
If PCsh.AutoFilterMode = True Then PCsh.AutoFilterMode = False
PCsh.Range("A1:K" & lastRow).AutoFilter Field:=7, Criteria1:="<" & CDate(beginDate)
If PCsh.Range("C2", Cells(PCsh.Rows.Count, "C").End(xlUp)).SpecialCells(xlCellTypeVisible).Cells(1, 1).Row > 1 Then
PCsh.Range("A2:K" & lastRow).SpecialCells(xlCellTypeVisible).EntireRow.Delete
End If
If PCsh.AutoFilterMode = True Then PCsh.AutoFilterMode = False
PCsh.Range("A1:K" & lastRow).AutoFilter Field:=7, Criteria1:=">" & CDate(endDate)
If PCsh.Range("A2", Cells(PCsh.Rows.Count, "A").End(xlUp)).SpecialCells(xlCellTypeVisible).Cells(1, 1).Row > 1 Then
PCsh.Range("A2:K" & lastRow).SpecialCells(xlCellTypeVisible).EntireRow.Delete
End If
If PCsh.AutoFilterMode = True Then PCsh.AutoFilterMode = False
RCsh.Activate
RCsh.Columns.AutoFit
RCsh.Range("A1:K" & lastRow).AutoFilter Field:=7, Criteria1:="<" & CDate(beginDate)
If RCsh.Range("C2", Cells(RCsh.Rows.Count, "C").End(xlUp)).SpecialCells(xlCellTypeVisible).Cells(1, 1).Row > 1 Then
RCsh.Range("A2:K" & lastRow).SpecialCells(xlCellTypeVisible).EntireRow.Delete
End If
If RCsh.AutoFilterMode = True Then RCsh.AutoFilterMode = False
RCsh.Range("A1:K" & lastRow).AutoFilter Field:=7, Criteria1:=">" & CDate(endDate)
If RCsh.Range("A2", Cells(RCsh.Rows.Count, "A").End(xlUp)).SpecialCells(xlCellTypeVisible).Cells(1, 1).Row > 1 Then
RCsh.Range("A2:K" & lastRow).SpecialCells(xlCellTypeVisible).EntireRow.Delete
End If
If RCsh.AutoFilterMode = True Then RCsh.AutoFilterMode = False
For Each ws In Sheets
If ws.Name Like "Overview*" Then
ws.UsedRange.Offset(1, 0).ClearContents
End If
Next ws
For Each ws In Sheets(Array("Purchase Costs", "Revenue Costs"))
For Each WIP In ws.Range("I2:I" & ws.Range("I" & ws.Rows.Count).End(xlUp).Row)
WIP.EntireRow.Copy Sheets("Overview WIP " & WIP.Value).Cells(Sheets("Overview WIP " & WIP.Value).Range("B" & Sheets("Overview WIP " & WIP.Value).Rows.Count).End(xlUp).Row + 1, 1)
Next WIP
Next ws
Application.ScreenUpdating = True
End Sub