Mã nguồn PHP:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
On Error Resume Next
Dim jZ As Long
If Not Intersect(Target, Range("A2:A999")) Is Nothing And Target = 0 Then
With Target
.Offset(, 6) = 0: jZ = .Row
.Offset(, 7) = .Offset(, 1)
End With
Do
jZ = jZ - 1
If IsNumeric(Cells(jZ, 1)) = False Then
Cells(jZ, 7).Interior.ColorIndex = Cells(jZ, 1).Interior.ColorIndex
Cells(jZ, 7) = Cells(jZ, 1): Exit Do
End If
Cells(jZ, 7) = Cells(jZ + 1, 7) + Cells(jZ, 1)
Cells(jZ, 8) = Cells(jZ + 1, 8) + Cells(jZ, 2)
Loop
jZ = Target.Row
Do
jZ = jZ + 1
If IsNumeric(Cells(jZ, 1)) = False Then
Cells(jZ, 7).Interior.ColorIndex = Cells(jZ, 1).Interior.ColorIndex
Cells(jZ, 7) = Cells(jZ, 1): Exit Do
End If
Cells(jZ, 7) = Cells(jZ - 1, 7) + Cells(jZ, 1)
Cells(jZ, 8) = Cells(jZ - 1, 8) + Cells(jZ, 2)
Loop
End If
End Sub
Hướng dẫn bạn 1 chút:
* Macro đang trong sheets("NguyenGoc") & mình đã thử chạy nó bằng cách tại ô A8 mình nhập số '0' & {ENTER}, macro đã thể hiện cách làm theo iêu cầu của bạn;
Còn 1 vài số '0' trên cột 'A', bạn có thể tiếp tục thử cho thành thục;
* Mình chưa rõ lắm; như theo quy trình thì dẫy số trên & dưới số 0 trên cột 'A' đã phải có? Nếu không đủ thì macro sẽ thực thi nữa vời mà thôi!
Chờ tin bạn!