=IF(H10=0,0,((SUMIF(Ma!$A$7:$A$19,NX!C10,Ma!$D$7:$D$19)+SUMIF($C$9:C9,C10,$G$9:G9)-SUMIF($C$9:C9,C10,$J$9:J9))/(SUMIF(Ma!$A$7:$A$19,NX!C10,Ma!$C$7:$C$19)+SUMIF($C$9:C9,C10,$F$9:F9)-SUMIF($C$9:C9,C10,$H$9:H9))))
=SUMIF(Ma!$A$7:$A$19,NX!C10,Ma!$C$7:$C$19)+SUMIF($C$10:C10,C10,$F$10:F10)-SUMIF($C$10:C10,C10,$H$10:H10)
=SUMIF(Ma!$A$7:$A$19,NX!C10,Ma!$D$7:$D$19)+SUMIF($C$10:C10,C10,$G$10:G10)-SUMIF($C$10:C10,C10,$J$10:J10)
Public Sub Gia_Von()
Dim Dic As Object, sArr(), dArr(), I As Long, J As Long, K As Long, Tem As String, N
Set Dic = CreateObject("Scripting.Dictionary")
With Sheets("Ma")
sArr = .Range(.[A7], .[A7].End(xlDown)).Resize(, 4).Value
End With
ReDim dArr(1 To UBound(sArr, 1), 1 To 9)
For I = 1 To UBound(sArr, 1)
Tem = sArr(I, 1)
If Not Dic.Exists(Tem) Then
K = K + 1
Dic.Add Tem, K
dArr(K, 1) = sArr(I, 3) ' So luong ton dau ky
dArr(K, 2) = sArr(I, 4) ' Thanh tien ton dau ky
End If
Next I
With Sheets("NX")
sArr = .Range(.[A9], .[A9].End(xlDown)).Resize(, 15).Value
End With
For I = 1 To UBound(sArr, 1)
Tem = sArr(I, 3)
If Dic.Exists(Tem) Then
N = Dic.Item(Tem)
dArr(N, 1) = dArr(N, 1) + sArr(I, 5) - sArr(I, 7) ' Tong so luong cong don
dArr(N, 2) = dArr(N, 2) + sArr(I, 6) - sArr(I, 9) ' Tong thanh tien cong don
dArr(N, 3) = dArr(N, 2) + sArr(I, 6) - sArr(I, 9) / dArr(N, 1) + sArr(I, 5) - sArr(I, 7) ' Gia von
End If
Next I
Set Dic = Nothing
End Sub
Đây là ý tưởng của em, nhưng chưa biết thể hiện làm sao cho đúng
Sub update()
Dim d As Object, d2 As Object
Dim hangton
Dim i, j
Set d = CreateObject("scripting.dictionary")
Set d2 = CreateObject("scripting.dictionary")
hangton = Sheet2.Range("A7:D" & Sheet2.Range("D" & Sheet2.Rows.Count).End(3).Row)
Application.ScreenUpdating = False
For i = 1 To UBound(hangton)
If Not d.exists(hangton(i, 1)) Then
d.Add hangton(i, 1), hangton(i, 3)
d2.Add hangton(i, 1), hangton(i, 4)
End If
Next
With Sheet1
.Range("I10:L200").ClearContents
For j = 10 To 200
If .Cells(j, 3) <> "" Then
If d.exists(.Cells(j, 3).Value) Then
If .Cells(j, 6) <> "" Then
d.Item(.Cells(j, 3).Value) = d.Item(.Cells(j, 3).Value) + .Cells(j, 6).Value
d2.Item(.Cells(j, 3).Value) = d2.Item(.Cells(j, 3).Value) + .Cells(j, 7).Value
.Cells(j, 11) = d.Item(.Cells(j, 3).Value)
.Cells(j, 12) = d2.Item(.Cells(j, 3).Value)
Else
.Cells(j, 9) = d2.Item(.Cells(j, 3).Value) / d.Item(.Cells(j, 3).Value)
.Cells(j, 10) = .Cells(j, 8) * .Cells(j, 9)
d.Item(.Cells(j, 3).Value) = d.Item(.Cells(j, 3).Value) - .Cells(j, 8).Value
d2.Item(.Cells(j, 3).Value) = d2.Item(.Cells(j, 3).Value) - .Cells(j, 10).Value
.Cells(j, 11) = d.Item(.Cells(j, 3).Value)
.Cells(j, 12) = d2.Item(.Cells(j, 3).Value)
End If
End If
End If
Next
End With
Application.ScreenUpdating = True
End Sub
Mình không hiểu về KT nhưng làm thấy kết quả giông giống nhau nên gửi cho bạn tham khảo
PHP:Sub update() Dim d As Object, d2 As Object Dim hangton Dim i, j Set d = CreateObject("scripting.dictionary") Set d2 = CreateObject("scripting.dictionary") hangton = Sheet2.Range("A7:D" & Sheet2.Range("D" & Sheet2.Rows.Count).End(3).Row) Application.ScreenUpdating = False For i = 1 To UBound(hangton) If Not d.exists(hangton(i, 1)) Then d.Add hangton(i, 1), hangton(i, 3) d2.Add hangton(i, 1), hangton(i, 4) End If Next With Sheet1 .Range("I10:L200").ClearContents For j = 10 To 200 If .Cells(j, 3) <> "" Then If d.exists(.Cells(j, 3).Value) Then If .Cells(j, 6) <> "" Then d.Item(.Cells(j, 3).Value) = d.Item(.Cells(j, 3).Value) + .Cells(j, 6).Value d2.Item(.Cells(j, 3).Value) = d2.Item(.Cells(j, 3).Value) + .Cells(j, 7).Value .Cells(j, 11) = d.Item(.Cells(j, 3).Value) .Cells(j, 12) = d2.Item(.Cells(j, 3).Value) Else .Cells(j, 9) = d2.Item(.Cells(j, 3).Value) / d.Item(.Cells(j, 3).Value) .Cells(j, 10) = .Cells(j, 8) * .Cells(j, 9) d.Item(.Cells(j, 3).Value) = d.Item(.Cells(j, 3).Value) - .Cells(j, 8).Value d2.Item(.Cells(j, 3).Value) = d2.Item(.Cells(j, 3).Value) - .Cells(j, 10).Value .Cells(j, 11) = d.Item(.Cells(j, 3).Value) .Cells(j, 12) = d2.Item(.Cells(j, 3).Value) End If End If End If Next End With Application.ScreenUpdating = True End Sub
Đúng yêu cầu mình roài, cảm ơn bạn nhiều nhé.Trước hết, cảm ơn bạn nhiều nhé bài viết và phản hồi đầu tiên.
Mình sẽ test thử
Em cũng đã làm như vậy, và tốc độ cũng ok rồi, cảm ơn bác nhiều nhéOK !
Nếu thấy code chạy chậm thì sửa lại cho for...next trên mảng rồi dán lại vào sheet nhé!