Option Explicit
Sub countifVBA()
Dim i&, rng, dic As Object, id As String
Set dic = CreateObject("Scripting.Dictionary")
rng = Range("D1:F" & Cells(Rows.Count, "D").End(xlUp).Row).Value
For i = 1 To UBound(rng)
id = rng(i, 1) & "|" & rng(i, 3)
If Not dic.exists(id) Then
dic.Add id, 1
Else
dic(id) = dic(id) + 1
End If
Next
For i = 1 To UBound(rng)
id = rng(i, 1) & "|" & rng(i, 3)
Cells(i, "H").Value = dic(id)
Next
End Sub
Cảm ơn bạn siêu nhiềuBạn nghiên cứu dùng Dictionary nhé
1 đoạn code ví dụ:
View attachment 291487PHP:Option Explicit Sub countifVBA() Dim i&, rng, dic As Object, id As String Set dic = CreateObject("Scripting.Dictionary") rng = Range("D1:F" & Cells(Rows.Count, "D").End(xlUp).Row).Value For i = 1 To UBound(rng) id = rng(i, 1) & "|" & rng(i, 3) If Not dic.exists(id) Then dic.Add id, 1 Else dic(id) = dic(id) + 1 End If Next For i = 1 To UBound(rng) id = rng(i, 1) & "|" & rng(i, 3) Cells(i, "H").Value = dic(id) Next End Sub