Private Sub Worksheet_Change(ByVal Target As Range)
Dim i As Long, cell As Range
Dim lastRow As Long, count As Long
Dim tdc As Variant, nth As Variant
Dim bc As Worksheet, d As Worksheet, r As Worksheet
Dim sumValue As Double
Dim u As Range 'Used to store all the cells needing to be cleared
Set u = Range("zz1000000") 'Empty cell
If Not Intersect(Target, Range("D" & lastRow)) Is Nothing Then Exit Sub
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Application.EnableEvents = False
tdc = Array("C", "D", "E", "F", "H", "I", "J")
nth = Array("I", "J", "K")
Set bc = Worksheets("Baocao")
lastRow = Cells(Rows.count, "D").End(xlUp).Row
'moved these out of the For Each cell because it is the same result either way and not calculated 298 times
count = Application.WorksheetFunction.count(Range("C2:C300")) '<<<
bc.Range("O7").Value = count '<<<
If lastRow >= 2 Then
sumValue = Application.WorksheetFunction.Sum(Range("G2:G300")) / 1000 '<<<
bc.Range("P7").Value = sumValue
End If
'There is no need for this for each cell
' For Each cell In Intersect(Range("D2:D300"), Target).Cells
'
' Next cell
For Each cell In Intersect(Range("D2:D300"), Target).Cells
If cell.Value = "" Then
Set u = Union(u, cell.Offset(, -3), cell.Offset(, 8), cell.Offset(, 9), cell.Offset(, 10))
ElseIf cell.Value <> "" Then
cell.Offset(, -3).Value = Range("N1").Value
End If
Next cell
'Got rid of the For loop. I think this is faster for this section
i = 2
Range("L" & i).Formula = "=IFERROR(LEFT(D" & i & ", LEN(D" & i & ") - 5), """")"
Range("M" & i).Formula = "=IFERROR(RIGHT(D" & i & ", 4), """")"
Range("N" & i).Formula = "=IFERROR(left(D" & i & ", 3), """")"
Range("L" & i).Copy Range("L" & i & ":" & "L" & lastRow)
Range("M" & i).Copy Range("M" & i & ":" & "M" & lastRow)
Range("N" & i).Copy Range("N" & i & ":" & "N" & lastRow)
Range("L" & i & ":" & "L" & lastRow).Value = Range("L" & i & ":" & "L" & lastRow).Value
Range("M" & i & ":" & "M" & lastRow).Value = Range("M" & i & ":" & "M" & lastRow).Value
Range("N" & i & ":" & "N" & lastRow).Value = Range("N" & i & ":" & "N" & lastRow).Value
For i = 24 To 30
For Each cl In tdc
bc.Range(cl & i).Value = WorksheetFunction.SumIfs(Range("G2:G300"), Range("L2:L300"), bc.Range("B" & i).Value, _
Range("M2:M300"), bc.Range(cl & "22").Value) / 1000
Next cl
Next i
For i = 14 To 18
For Each cl In nth
bc.Range(cl & i).Value = WorksheetFunction.SumIfs(Range("G2:G300"), Range("L2:L300"), bc.Range("H" & i).Value, _
Range("M2:M300"), bc.Range(cl & "13").Value) / 1000
Next cl
Next i
For Each cell In Range("B2:B" & Rows.count)
' Check if the cell value is "Toa"
If cell.Value = "Toa" Then
' Get the row number of the cell
row_num = cell.Row
' Loop through columns A to K for the row and delete the cell values
For col = 1 To 11
Set u = Union(u, Cells(row_num, col))
'Cells(row_num, col).Value = ""
Next col
End If
Next cell
u.Value = "" 'Set all the cells to blank at once
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
Application.EnableEvents = True
End Sub