Private Sub Worksheet_Change(ByVal Target As Range)
Dim LastRow As Long, wsLastRow As Long, opLastRow As Long
Dim ws As Worksheet
Dim TeamName As String, FindString As String
Dim Rng As Range, cRange As Range
If Target.Cells.Count > 1 Then Exit Sub
LastRow = ActiveSheet.Cells(Rows.Count, "C").End(xlUp).Row + 1
If Not Intersect(Target, Range("C2:C" & LastRow)) Is Nothing Then
If Target.Value <> "" Then
If Target.Offset(0, 1).Value = "" Then
TeamName = Target.Value
Set ws = Sheets(TeamName)
If ws.Range("A40") = "" Then
ActiveSheet.Range("A" & Target.Row).EntireRow.Copy ws.Range("A40").EntireRow
Else
wsLastRow = ws.Cells(Rows.Count, "A").End(xlUp).Row + 1
ActiveSheet.Range("A" & Target.Row).EntireRow.Copy ws.Range("A" & wsLastRow).EntireRow
End If
Else
FindString = Target.Offset(0, -2).Value
Set ws = Sheets(Target.Offset(0, 1).Value)
wsLastRow = ws.Cells(Rows.Count, "A").End(xlUp).Row + 1
Set cRange = ws.Range("A40:A" & wsLastRow)
With cRange
Set Rng = .Find(What:=FindString, _
After:=.Cells(1), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False)
If Not Rng Is Nothing Then
ActiveSheet.Range("A" & Target.Row).EntireRow.Copy ws.Range("A" & Rng.Row).EntireRow
End If
End With
TeamName = Target.Value
Set ws = Sheets(TeamName)
If ws.Range("A40") = "" Then
ActiveSheet.Range("A" & Target.Row).EntireRow.Copy ws.Range("A40").EntireRow
Else
wsLastRow = ws.Cells(Rows.Count, "A").End(xlUp).Row + 1
ActiveSheet.Range("A" & Target.Row).EntireRow.Copy ws.Range("A" & wsLastRow).EntireRow
End If
End If
End If
End If
If Not Intersect(Target, Range("D2:D" & LastRow)) Is Nothing Then
If Target.Value <> "" Then
If Target.Offset(0, -1).Value = "" Then
If Sheets("AM").Range("A40") = "" Then
ActiveSheet.Range("A" & Target.Row).EntireRow.Copy Sheets("AM").Range("A40").EntireRow
Else
opLastRow = Sheets("AM").Cells(Rows.Count, "A").End(xlUp).Row + 1
ActiveSheet.Range("A" & Target.Row).EntireRow.Copy Sheets("AM").Range("A" & wsLastRow).EntireRow
End If
Else
TeamName = Target.Offset(0, -1).Value
FindString = Target.Offset(0, -3).Value
Set ws = Sheets(TeamName)
wsLastRow = ws.Cells(Rows.Count, "A").End(xlUp).Row + 1
Set cRange = ws.Range("A40:A" & wsLastRow)
With cRange
Set Rng = .Find(What:=FindString, _
After:=.Cells(1), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False)
If Not Rng Is Nothing Then
ActiveSheet.Range("A" & Target.Row).EntireRow.Copy ws.Range("A" & Rng.Row).EntireRow
End If
End With
If Sheets("AM").Range("A40") = "" Then
ActiveSheet.Range("A" & Target.Row).EntireRow.Copy Sheets("AM").Range("A40").EntireRow
Else
FindString = Target.Offset(0, -3).Value
Set ws = Sheets(Target.Value)
wsLastRow = ws.Cells(Rows.Count, "A").End(xlUp).Row + 1
Set cRange = ws.Range("A40:A" & wsLastRow)
With cRange
Set Rng = .Find(What:=FindString, _
After:=.Cells(1), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False)
If Rng Is Nothing Then
opLastRow = Sheets("AM").Cells(Rows.Count, "A").End(xlUp).Row + 1
ActiveSheet.Range("A" & Target.Row).EntireRow.Copy Sheets("AM").Range("A" & wsLastRow).EntireRow
End If
End With
End If
End If
End If
End If
End Sub