Mã nguồn PHP:
Option Explicit: Option Base 1
Sub CopyAndSum()
Dim Rng As Range, lRow As Long
1
Range(Cells(1, 24), Cells(Cells(65432, 26).End(xlUp).Row, 26)).ClearContents
2
Set Rng = Range(Cells(2, 2), Cells(Cells(65432, 4).End(xlUp).Row, 4))
Rng.Copy Destination:=Cells(1, 24)
5
Set Rng = Range(Cells(3, 8), Cells(Cells(65432, 10).End(xlUp).Row + 1, 10))
Rng.Copy Destination:=Cells(Cells(65432, 24).End(xlUp).Row + 1, 24)
7
Set Rng = Range(Cells(1, 24), Cells(Cells(65432, 26).End(xlUp).Row, 26))
Rng.Select
Selection.Sort Key1:=Range("X2"), Order1:=xlAscending, Key2:=Range("Y2") _
, Order2:=xlAscending, Header:=xlGuess, OrderCustom:=1, MatchCase:= _
False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal, DataOption2 _
:=xlSortNormal
lRow = Cells(Cells(65432, 24).End(xlUp).Row, 24).Row
ReDim Mang(lRow, 3): Dim lDem As Long, iJ As Long
For iJ = 2 To lRow
With Cells(iJ, 24)
If iJ = 2 Then
lDem = 1: Mang(lDem, 1) = .Value
Mang(lDem, 2) = .Offset(, 1): Mang(lDem, 3) = .Offset(, 2)
Else
If (.Value = Mang(lDem, 1) And .Offset(, 1) <> Mang(lDem, 2)) Or _
.Value <> Mang(lDem, 1) Then
lDem = 1 + lDem: Mang(lDem, 1) = .Value
Mang(lDem, 2) = .Offset(, 1): Mang(lDem, 3) = .Offset(, 2)
ElseIf .Value = Mang(lDem, 1) And .Offset(, 1) = Mang(lDem, 2) Then '*!*'
Mang(lDem, 3) = Mang(lDem, 3) + .Offset(, 2)
End If
End If
End With
Next iJ
For iJ = 1 To lDem
With Cells(iJ + 2, 14)
.Value = Mang(iJ, 1): .Offset(, -1) = iJ
.Offset(, 1) = Mang(iJ, 2): .Offset(, 2) = Mang(iJ, 3)
End With
Next iJ
Range(Cells(1, 24), Cells(Cells(65432, 26).End(xlUp).Row, 26)).ClearContents
End Sub