dimanche 19 avril 2015

Timestamping and copying a line to another sheet, if certain condition met

I need my audit list to (1) add a time stamp in the end of current line and then (2) copy the line to the other sheet, when there is a "N" or "n" marked in the specified column. The idea is to get a summary of copied non-conformities.


My trouble is that in the case of the code I use, it only deals with the first column correctly. It does nothing with others.


I use the code (below).



Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo ErrHandler
Application.EnableEvents = False

If Target.Column = 9 And UCase(Target) = "N" Then
Set rChange = Intersect(Target, Range("I:I"))
If Not rChange Is Nothing Then
Application.EnableEvents = False
For Each rCell In rChange
If rCell > "" Then
With rCell.Offset(0, 2)
.Value = Now
.NumberFormat = "dd/mm/yyyy"
End With
Else
rCell.Offset(0, 2).Clear
End If
Next
End If

If Target.Column = 9 And UCase(Target) = "N" Then
Target.EntireRow.Copy Destination:=Sheet9.Range("A" & _
Rows.Count).End(xlUp).Offset(1)

If Target.Column = 8 And UCase(Target) = "N" Then
Set rChange = Intersect(Target, Range("H:H"))
If Not rChange Is Nothing Then
Application.EnableEvents = False
For Each rCell In rChange
If rCell > "" Then
With rCell.Offset(0, 3)
.Value = Now
.NumberFormat = "dd/mm/yyyy"
End With
Else
rCell.Offset(0, 3).Clear
End If
Next
End If
If Target.Column = 8 And UCase(Target) = "N" Then
Target.EntireRow.Copy Destination:=Sheet9.Range("A" & _
Rows.Count).End(xlUp).Offset(1)

If Target.Column = 7 And UCase(Target) = "N" Then
Set rChange = Intersect(Target, Range("G:G"))
If Not rChange Is Nothing Then
Application.EnableEvents = False
For Each rCell In rChange
If rCell > "" Then
With rCell.Offset(0, 4)
.Value = Now
.NumberFormat = "dd/mm/yyyy"
End With
Else
rCell.Offset(0, 4).Clear
End If
Next
End If
If Target.Column = 7 And UCase(Target) = "N" Then
Target.EntireRow.Copy Destination:=Sheet9.Range("A" & _
Rows.Count).End(xlUp).Offset(1)


If Target.Column = 6 And UCase(Target) = "N" Then
Set rChange = Intersect(Target, Range("F:F"))
If Not rChange Is Nothing Then
Application.EnableEvents = False
For Each rCell In rChange
If rCell > "" Then
With rCell.Offset(0, 5)
.Value = Now
.NumberFormat = "dd/mm/yyyy"
End With
Else
rCell.Offset(0, 5).Clear
End If
Next
End If
If Target.Column = 6 And UCase(Target) = "N" Then
Target.EntireRow.Copy Destination:=Sheet9.Range("A" & _
Rows.Count).End(xlUp).Offset(1)

If Target.Column = 5 And UCase(Target) = "N" Then
Set rChange = Intersect(Target, Range("E:E"))
If Not rChange Is Nothing Then
Application.EnableEvents = False
For Each rCell In rChange
If rCell > "" Then
With rCell.Offset(0, 6)
.Value = Now
.NumberFormat = "dd/mm/yyyy"
End With
Else
rCell.Offset(0, 6).Clear
End If
Next
End If
If Target.Column = 5 And UCase(Target) = "N" Then

Target.EntireRow.Copy Destination:=Sheet9.Range("A" & _
Rows.Count).End(xlUp).Offset(1)

If Target.Column = 4 And UCase(Target) = "N" Then
Set rChange = Intersect(Target, Range("D:D"))
If Not rChange Is Nothing Then
Application.EnableEvents = False
For Each rCell In rChange
If rCell > "" Then
With rCell.Offset(0, 7)
.Value = Now
.NumberFormat = "dd/mm/yyyy"
End With
Else
rCell.Offset(0, 7).Clear
End If
Next
End If
If Target.Column = 4 And UCase(Target) = "N" Then
Target.EntireRow.Copy Destination:=Sheet9.Range("A" & _
Rows.Count).End(xlUp).Offset(1)

End If
End If
End If
End If
End If
End If
End If

End If
End If
End If
End If
End If

ErrHandler:
Application.EnableEvents = True

End Sub

Aucun commentaire:

Enregistrer un commentaire