السلام عليكم
Private Sub Worksheet_Change(ByVal Target As Range)
On Error Resume Next
Dim R As Integer
If Not Intersect(Target.Cells(1, 1), Union(Range("D18:D39"), Range("F18:F39"), Range("O18:O39"))) Is Nothing Then
R = Target.Row
If Cells(R, "D").Value <> "" Then
Cells(R, "C").Value = R - 17
Cells(R, "G").Value = Val(IIf(Cells(R, "O") <> "", Cells(R, "O"), Cells(R, "N")))
Cells(R, "H").Value = Val(Cells(R, "F")) * Val(Cells(R, "G"))
Cells(R, "N").Value = WorksheetFunction.VLookup(Cells(R, "D"), [prices], 3, 0)
Cells(R, "P").Value = WorksheetFunction.VLookup(Cells(R, "D"), [prices], 4, 0)
Cells(R, "Q").Value = (Val(Cells(R, "G")) - Val(Cells(R, "P"))) * Val(Cells(R, "F"))
Else
Union(Cells(R, "C"), Cells(R, "H"), Cells(R, "N"), Cells(R, "P"), Cells(R, "Q")).ClearContents
End If
End If
On Error GoTo 0
End Sub