Hi, I have reached the limit of my ability on this one. My code checks for an order number and if it exists updates a tracking sheet with todays date. If the order number does not exist it creates a record on the tracking sheet and records todays date in the relevant status code. Works great for single instances however the user needs to be able to copy and paste a batch of statuses at once and the code is not running in that instance.
I am sure the select case can be written better however that is not the primary concern at the moment. Also, the error handling is there just to prevent error messages until I can sort out the functionality.
Can you please help me fire the Select Case when the change event is a copy & paste or even an autofill drag.
Thanks in advance.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim StatusRng As Range
Dim Rng As Range
Dim LastRow As Long, CurrRow As Long
Dim ws As Worksheet
On Error Resume Next
Set ws = Worksheets("Tracking")
Application.EnableEvents = True
Set StatusRng = Intersect(Application.ActiveSheet.Range("B3:B5000"), Target)
LastRow = ws.Cells(Rows.Count, "A").End(xlUp).Row + 1
If Not StatusRng Is Nothing Then
Application.EnableEvents = False
For Each Rng In StatusRng
If Not VBA.IsEmpty(Rng.Value) Then
'check if the production order is empty. If it is then exit the routine
If IsEmpty(Selection.Offset(0, 11)) Then
Application.EnableEvents = True
Exit Sub
Else
'check to see if the production order already exists in the Tracking sheet
result = Application.VLookup(Selection.Offset(0, 11).Value, Sheets("Tracking").Range("B2:B5000"), 1, False)
'if this is a new record then write to the Tracking sheet
If IsError(result) Then
Sheets("Tracking").Range("B" & LastRow) = Selection.Offset(0, 11).Value
CurrRow = LastRow
Select Case Rng.Value
Case "Materials"
ws.Range("C" & CurrRow) = Date
Case "Upcoming"
ws.Range("D" & CurrRow) = Date
Case "Ready"
ws.Range("E" & CurrRow) = Date
Case "Scheduled"
ws.Range("F" & CurrRow) = Date
Case "In Production"
ws.Range("G" & CurrRow) = Date
Case "Finished"
ws.Range("H" & CurrRow) = Date
Case "3rd Party"
ws.Range("I" & CurrRow) = Date
Case "Engineering"
ws.Range("J" & CurrRow) = Date
Case "Cancelled"
ws.Range("K" & CurrRow) = Date
Case "Hold"
ws.Range("L" & CurrRow) = Date
End Select
ws.Range("A" & LastRow) = LastRow
Else
'Apend the existing record with the updated status
CurrRow = Application.WorksheetFunction.Match(result, ws.Range("B:B"), 0)
Select Case Rng.Value
Case "Materials"
ws.Range("C" & CurrRow) = Date
Case "Upcoming"
ws.Range("D" & CurrRow) = Date
Case "Ready"
ws.Range("E" & CurrRow) = Date
Case "Scheduled"
ws.Range("F" & CurrRow) = Date
Case "In Production"
ws.Range("G" & CurrRow) = Date
Case "Finished"
ws.Range("H" & CurrRow) = Date
Case "3rd Party"
ws.Range("I" & CurrRow) = Date
Case "Engineering"
ws.Range("J" & CurrRow) = Date
Case "Cancelled"
ws.Range("K" & CurrRow) = Date
Case "Hold"
ws.Range("L" & CurrRow) = Date
End Select
End If
End If
End If
Next
Application.EnableEvents = True
End If
End Sub
Display More