Sub CopyData()
Application.ScreenUpdating = False
Dim LastRow As Long, srcWS As Worksheet, v As Variant, i As Long, x As Long
Set srcWS = Sheets("Hankerekisteri")
v = srcWS.Range("A4", srcWS.Range("A" & Rows.Count).End(xlUp))
For i = 1 To UBound(v, 1)
x = srcWS.Range("A" & i + 3).DisplayFormat.Interior.Color
With srcWS
.ListObjects("tblhankkeet").Range.AutoFilter Field:=1, Criteria1:=x, Operator:=xlFilterCellColor
Select Case x
Case Is = 49407 'orange
With Sheets("Keskeneräiset hankkeet")
LastRow = .Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
If LastRow > 3 Then
.UsedRange.Rows("4:" & LastRow).Delete
End If
srcWS.Range("A4", srcWS.Range("I" & Rows.Count).End(xlUp)).SpecialCells(xlCellTypeVisible).Copy .Range("A4")
Application.DisplayAlerts = False
srcWS.Range("A4", srcWS.Range("I" & Rows.Count).End(xlUp)).SpecialCells(xlCellTypeVisible).Delete
Application.DisplayAlerts = True
End With
Case Is = 9359529 'green
With Sheets("Valmiit hankkeet")
LastRow = .Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
If LastRow > 3 Then
.UsedRange.Rows("4:" & LastRow).Delete
End If
srcWS.Range("A4", srcWS.Range("I" & Rows.Count).End(xlUp)).SpecialCells(xlCellTypeVisible).Copy .Range("A4")
Application.DisplayAlerts = False
srcWS.Range("A4", srcWS.Range("I" & Rows.Count).End(xlUp)).SpecialCells(xlCellTypeVisible).Delete
Application.DisplayAlerts = True
End With
Case Is = 13311 'red
With Sheets("Peruutetut hankkeet")
LastRow = .Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
If LastRow > 3 Then
.UsedRange.Rows("4:" & LastRow).Delete
End If
srcWS.Range("A4", srcWS.Range("I" & Rows.Count).End(xlUp)).SpecialCells(xlCellTypeVisible).Copy .Range("A4")
Application.DisplayAlerts = False
srcWS.Range("A4", srcWS.Range("I" & Rows.Count).End(xlUp)).SpecialCells(xlCellTypeVisible).Delete
Application.DisplayAlerts = True
End With
End Select
End With
Next i
srcWS.ListObjects("tblhankkeet").Range.AutoFilter
Application.ScreenUpdating = True
End Sub