Hello Everyone,
I am writing VBA code to track machine downtime (Excel file with VBA code in the attachment). My question is on how to use a target single cell to update columns continuously. Below shares what I want to achieve:
(1) PLC writes machine status to cell "B2" which is "1" or "0" meaning machine running or not running respectively. At start of production, Immediately "B2" is "1", cell "D2" is updated with start date and time. In the event of stop, "B2" value changes to "0" and cell "C2" updated with stop date and time and the 'stop reason' is also updated simultaneously on cell "K2". This has been achieved. See code below.
(2) The challenge now is when next the machine starts, and cell "B2" value changed from "0" to "1", how do I make this change to update cell "D3", and then cell "C3" and "K3" when next machine stops. I want this to continue for cell "D4","C4" and "C5" and so on as cell "B2" value changes between "1" and "0"
Number 1 above has been achieved with below code:
Code_Goes_Here
Private Sub Worksheet_Change(ByVal Target As Range)
'Downtime tracking
Dim i As Integer
For i = 2 To 1000
If Target.Address = Range("C2").Address And Cells(i, "C").Value <> "" And Cells(i, "C").Value = "1" And Cells(i, "D").Value = "" And Cells(i, "E").Value = "" And Cells(i, "K").Value = "" Then
Cells(i, "D").Value = Date & " " & Time
Cells(i, "D").NumberFormat = "dd/mm/yy hh:mm:ss"
End If
If Target.Address = Range("C2").Address And Cells(i, "C").Value <> "" And Cells(i, "C").Value <> ">=1" And Cells(i, "C").Value = "0" And Cells(i, "D").Value <> "" And Cells(i, "E").Value = "" And Cells(i, "K").Value = "" Then
Cells(i, "E").Value = Date & " " & Time
Cells(i, "E").NumberFormat = "dd/mm/yy hh:mm:ss"
Cells(i, "K").Value = Range("B3").Value
End If
Next
Range("D:D").EntireColumn.AutoFit
Range("E:E").EntireColumn.AutoFit
Range("K:K").EntireColumn.AutoFit
End Sub