Cách sử dung hàm Countif trong VBA

Liên hệ QC

Excel365

Thành viên tích cực
Tham gia
29/10/10
Bài viết
862
Được thích
125
Giới tính
Nam
Các anh (chị) cho em hỏi cách sử dụng hàm Countif trong VBA để đếm theo điều kiện trong vùng.
 
Các anh (chị) cho em hỏi cách sử dụng hàm Countif trong VBA để đếm theo điều kiện trong vùng.
Cú pháp: WorksheetFunction.CountIf(Vùng,Điều_kiện)
Ví dụ:
PHP:
a = WorksheetFunction.CountIf(Sheets("Data").[A2:A100],">10")
PHP:
b = WorksheetFunction.CountIf(Sheet1.Range("A2:A100"),"<>0")
 
Xin giúp thu gọn đoạn marco trong excel sau đây. Cám ơn các bạn rất nhiều.
'n = 2
' Kiem tra trung tiet tren dong 2
k = 1 'gan bien k
Do While k < 41
b = WorksheetFunction.countif([A2:AQ2], m(k))
If b > 1 Then MsgBox "So tiet trung tren dong " & " 2 " & " cua " & m(k) & " la " & b
k = k + 1
Loop

'n = 3
' Kiem tra trung tiet tren dong 3
k = 1 'gan bien k
Do While k < 41
b = WorksheetFunction.countif([A3:AQ3], m(k))
If b > 1 Then MsgBox "So tiet trung tren dong " & " 3 " & " cua " & m(k) & " la " & b
k = k + 1
Loop

'n = 4
' Kiem tra trung tiet tren dong 4
k = 1 'gan bien k
Do While k < 41
b = WorksheetFunction.countif([A4:AQ4], m(k))
If b > 1 Then MsgBox "So tiet trung tren dong " & " 4 " & " cua " & m(k) & " la " & b
k = k + 1
Loop

'n = 5
' Kiem tra trung tiet tren dong 2
k = 1 'gan bien k
Do While k < 41
b = WorksheetFunction.countif([A5:AQ5], m(k))
If b > 1 Then MsgBox "So tiet trung tren dong " & " 5 " & " cua " & m(k) & " la " & b
k = k + 1
Loop

'n = 6
' Kiem tra trung tiet tren dong 2
k = 1 'gan bien k
Do While k < 41
b = WorksheetFunction.countif([A6:AQ6], m(k))
If b > 1 Then MsgBox "So tiet trung tren dong " & " 6 " & " cua " & m(k) & " la " & b
k = k + 1
Loop
 
Xin giúp thu gọn đoạn marco trong excel sau đây. Cám ơn các bạn rất nhiều.
'n = 2
' Kiem tra trung tiet tren dong 2
k = 1 'gan bien k
Do While k < 41
b = WorksheetFunction.countif([A2:AQ2], m(k))
If b > 1 Then MsgBox "So tiet trung tren dong " & " 2 " & " cua " & m(k) & " la " & b
k = k + 1
Loop

'n = 3
' Kiem tra trung tiet tren dong 3
k = 1 'gan bien k
Do While k < 41
b = WorksheetFunction.countif([A3:AQ3], m(k))
If b > 1 Then MsgBox "So tiet trung tren dong " & " 3 " & " cua " & m(k) & " la " & b
k = k + 1
Loop

'n = 4
' Kiem tra trung tiet tren dong 4
k = 1 'gan bien k
Do While k < 41
b = WorksheetFunction.countif([A4:AQ4], m(k))
If b > 1 Then MsgBox "So tiet trung tren dong " & " 4 " & " cua " & m(k) & " la " & b
k = k + 1
Loop

'n = 5
' Kiem tra trung tiet tren dong 2
k = 1 'gan bien k
Do While k < 41
b = WorksheetFunction.countif([A5:AQ5], m(k))
If b > 1 Then MsgBox "So tiet trung tren dong " & " 5 " & " cua " & m(k) & " la " & b
k = k + 1
Loop

'n = 6
' Kiem tra trung tiet tren dong 2
k = 1 'gan bien k
Do While k < 41
b = WorksheetFunction.countif([A6:AQ6], m(k))
If b > 1 Then MsgBox "So tiet trung tren dong " & " 6 " & " cua " & m(k) & " la " & b
k = k + 1
Loop
Bạn thử:
PHP:
Dim i As Integer, k
    k = 1    'gan bien k
    Do While k < 41
        For i = 2 To 6
            b = WorksheetFunction.CountIf(Range("A1:AQ1").Offset(i), m(k))
            If b > 1 Then MsgBox "So tiet trung tren dong " & " i " & " cua " & m(k) & " la " & b
            k = k + 1
        Next i
    Loop
 
Dim i As Integer, k
k = 1 'gan bien k
Do While k < 41
For i = 2 To 6
b = WorksheetFunction.CountIf(Range("A1:AQ1").Offset(i), m(k))
If b > 1 Then MsgBox "So tiet trung tren dong " & " i " & " cua " & m(k) & " la " & b
k = k + 1
Next i
Loop
Cám ơn bạn rất nhiều, bạn đã giúp mình sử offset.
Song nó còn trục trặc đâu đó : Không chạy được nhưng khi thay đổi dòng next i lên trên dòng k=k+1 thì chạy nhưng Msbbox ..& " i " &... vẫn ghi là i chứ không ghi 2 , 3 , ... theo từng bước for bạn ơi.
Cám ơn bạn.
Bài đã được tự động gộp:

Sorry & i & chứ không phải & "i" &. Cám ơn
 
Lần chỉnh sửa cuối:
Các thầy giúp em thu gọn code này với ạ. em cảm ơn

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Application.ScreenUpdating = False
'Bo qua loi
On Error Resume Next
'TONG HOP HOC LUC
Range("C6").Value = WorksheetFunction.CountIfs("bieutong12").[f11:f100],B6)
Range("C6").Value = "=COUNTIFS(bieutong12!f11:f100,B6)" 'tieng viet
Range("C7").Value = "=COUNTIFS(bieutong12!f11:f100,B7)"
Range("C8").Value = "=COUNTIFS(bieutong12!f11:f100,B8)"
Range("C9").Value = "=COUNTIFS(bieutong12!f11:f100,B9)"

Range("D6").Value = "=COUNTIFS(bieutong12!h11:h100,B6)" 'toan
Range("D7").Value = "=COUNTIFS(bieutong12!h11:h100,B7)"
Range("D8").Value = "=COUNTIFS(bieutong12!h11:h100,B8)"
Range("D9").Value = "=COUNTIFS(bieutong12!h11:h100,B9)"

Range("E6").Value = "=COUNTIFS(bieutong12!j11:j100,B6)" 'TNXH
Range("E7").Value = "=COUNTIFS(bieutong12!j11:j100,B7)"
Range("E8").Value = "=COUNTIFS(bieutong12!j11:j100,B8)"
Range("E9").Value = "=COUNTIFS(bieutong12!j11:j100,B9)"

Range("f6").Value = "=COUNTIFS(bieutong12!k11:k100,B6)" 'Ngoai ngu
Range("f7").Value = "=COUNTIFS(bieutong12!k11:k100,B7)"
Range("f8").Value = "=COUNTIFS(bieutong12!k11:k100,B8)"
Range("f9").Value = "=COUNTIFS(bieutong12!k11:k100,B9)"

Range("g6").Value = "=COUNTIFS(bieutong12!m11:m100,B6)" 'Tin hoc
Range("g7").Value = "=COUNTIFS(bieutong12!m11:m100,B7)"
Range("g8").Value = "=COUNTIFS(bieutong12!m11:m100,B8)"
Range("g9").Value = "=COUNTIFS(bieutong12!m11:m100,B9)"

Range("h6").Value = "=COUNTIFS(bieutong12!o11:eek:100,B6)" 'Tieng dan toc
Range("h7").Value = "=COUNTIFS(bieutong12!o11:eek:100,B7)"
Range("h8").Value = "=COUNTIFS(bieutong12!o11:eek:100,B8)"
Range("h9").Value = "=COUNTIFS(bieutong12!o11:eek:100,B9)"

Range("i6").Value = "=COUNTIFS(bieutong12!q11:q100,B6)" 'Dao duc
Range("i7").Value = "=COUNTIFS(bieutong12!q11:q100,B7)"
Range("i8").Value = "=COUNTIFS(bieutong12!q11:q100,B8)"
Range("i9").Value = "=COUNTIFS(bieutong12!q11:q100,B9)"

Range("j6").Value = "=COUNTIFS(bieutong12!r11:r100,B6)" 'Am nhac
Range("j7").Value = "=COUNTIFS(bieutong12!r11:r100,B7)"
Range("j8").Value = "=COUNTIFS(bieutong12!r11:r100,B8)"
Range("j9").Value = "=COUNTIFS(bieutong12!r11:r100,B9)"

Range("k6").Value = "=COUNTIFS(bieutong12!s11:s100,B6)" 'my thuat
Range("k7").Value = "=COUNTIFS(bieutong12!s11:s100,B7)"
Range("k8").Value = "=COUNTIFS(bieutong12!s11:s100,B8)"
Range("k9").Value = "=COUNTIFS(bieutong12!s11:s100,B9)"

Range("l6").Value = "=COUNTIFS(bieutong12!t11:t100,B6)" 'HDTN
Range("l7").Value = "=COUNTIFS(bieutong12!t11:t100,B7)"
Range("l8").Value = "=COUNTIFS(bieutong12!t11:t100,B8)"
Range("l9").Value = "=COUNTIFS(bieutong12!t11:t100,B9)"

Range("M6").Value = "=COUNTIFS(bieutong12!u11:u100,B6)" 'the chat
Range("M7").Value = "=COUNTIFS(bieutong12!u11:u100,B7)"
Range("M8").Value = "=COUNTIFS(bieutong12!u11:u100,B8)"
Range("M9").Value = "=COUNTIFS(bieutong12!u11:u100,B9)"

Range("c6:m9").Value = Range("c6:m9").Value 'chuyen toan bo sang value

'TONG HOP NANG LUC chung
Range("C15").Value = "=COUNTIFS(bieutong12!v11:v100,B15)" 'tu quan
Range("C16").Value = "=COUNTIFS(bieutong12!v11:v100,B16)"
Range("C17").Value = "=COUNTIFS(bieutong12!v11:v100,B17)"


Range("D15").Value = "=COUNTIFS(bieutong12!w11:w100,B15)" 'Giao tiep
Range("D16").Value = "=COUNTIFS(bieutong12!w11:w100,B16)"
Range("D17").Value = "=COUNTIFS(bieutong12!w11:w100,B17)"

Range("E15").Value = "=COUNTIFS(bieutong12!x11:x100,B15)" 'Giai quyet van de
Range("E16").Value = "=COUNTIFS(bieutong12!x11:x100,B16)"
Range("E17").Value = "=COUNTIFS(bieutong12!x11:x100,B17)"

'nang luc dac thu
Range("F15").Value = "=COUNTIFS(bieutong12!y11:y100,B15)" 'Ngon ngu
Range("F16").Value = "=COUNTIFS(bieutong12!y11:y100,B16)"
Range("F17").Value = "=COUNTIFS(bieutong12!y11:y100,B17)"

Range("G15").Value = "=COUNTIFS(bieutong12!z11:z100,B15)" 'toan hoc
Range("G16").Value = "=COUNTIFS(bieutong12!z11:z100,B16)"
Range("G17").Value = "=COUNTIFS(bieutong12!z11:z100,B17)"

Range("H15").Value = "=COUNTIFS(bieutong12!Aa11:Aa100,B15)" 'tham my
Range("H16").Value = "=COUNTIFS(bieutong12!Aa11:Aa100,B16)"
Range("H17").Value = "=COUNTIFS(bieutong12!Aa11:Aa100,B17)"

Range("i15").Value = "=COUNTIFS(bieutong12!Ab11:Ab100,B15)" 'the chat
Range("i16").Value = "=COUNTIFS(bieutong12!Ab11:Ab100,B16)"
Range("i17").Value = "=COUNTIFS(bieutong12!Ab11:Ab100,B17)"

'Pham chat

Range("j15").Value = "=COUNTIFS(bieutong12!Ac11:Ac100,B15)" 'Yeu nuoc
Range("j16").Value = "=COUNTIFS(bieutong12!Ac11:Ac100,B16)"
Range("j17").Value = "=COUNTIFS(bieutong12!Ac11:Ac100,B17)"


Range("k15").Value = "=COUNTIFS(bieutong12!Ad11:Ad100,B15)" 'Nhan ai
Range("k16").Value = "=COUNTIFS(bieutong12!Ad11:Ad100,B16)"
Range("k17").Value = "=COUNTIFS(bieutong12!Ad11:Ad100,B17)"

Range("L15").Value = "=COUNTIFS(bieutong12!Ae11:Ae100,B15)" 'Cham chi
Range("L16").Value = "=COUNTIFS(bieutong12!Ae11:Ae100,B16)"
Range("L17").Value = "=COUNTIFS(bieutong12!Ae11:Ae100,B17)"

Range("M15").Value = "=COUNTIFS(bieutong12!Af11:Af100,B15)" 'Trung thuc
Range("M16").Value = "=COUNTIFS(bieutong12!Af11:Af100,B16)"
Range("M17").Value = "=COUNTIFS(bieutong12!Af11:Af100,B17)"

Range("N15").Value = "=COUNTIFS(bieutong12!Ag11:Ag100,B15)" 'trach nhiem
Range("N16").Value = "=COUNTIFS(bieutong12!Ag11:Ag100,B16)"
Range("N17").Value = "=COUNTIFS(bieutong12!Ag11:Ag100,B17)"


Range("C15:N17").Value = Range("C15:N17").Value
'Mo lai bao loi
On Error GoTo 0
Application.ScreenUpdating = True
End Sub
 
Các thầy giúp em thu gọn code này với ạ. em cảm ơn

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Application.ScreenUpdating = False
'Bo qua loi
On Error Resume Next
'TONG HOP HOC LUC
Range("C6").Value = WorksheetFunction.CountIfs("bieutong12").[f11:f100],B6)
Range("C6").Value = "=COUNTIFS(bieutong12!f11:f100,B6)" 'tieng viet
Range("C7").Value = "=COUNTIFS(bieutong12!f11:f100,B7)"
Range("C8").Value = "=COUNTIFS(bieutong12!f11:f100,B8)"
Range("C9").Value = "=COUNTIFS(bieutong12!f11:f100,B9)"

Range("D6").Value = "=COUNTIFS(bieutong12!h11:h100,B6)" 'toan
Range("D7").Value = "=COUNTIFS(bieutong12!h11:h100,B7)"
Range("D8").Value = "=COUNTIFS(bieutong12!h11:h100,B8)"
Range("D9").Value = "=COUNTIFS(bieutong12!h11:h100,B9)"

Range("E6").Value = "=COUNTIFS(bieutong12!j11:j100,B6)" 'TNXH
Range("E7").Value = "=COUNTIFS(bieutong12!j11:j100,B7)"
Range("E8").Value = "=COUNTIFS(bieutong12!j11:j100,B8)"
Range("E9").Value = "=COUNTIFS(bieutong12!j11:j100,B9)"

Range("f6").Value = "=COUNTIFS(bieutong12!k11:k100,B6)" 'Ngoai ngu
Range("f7").Value = "=COUNTIFS(bieutong12!k11:k100,B7)"
Range("f8").Value = "=COUNTIFS(bieutong12!k11:k100,B8)"
Range("f9").Value = "=COUNTIFS(bieutong12!k11:k100,B9)"

Range("g6").Value = "=COUNTIFS(bieutong12!m11:m100,B6)" 'Tin hoc
Range("g7").Value = "=COUNTIFS(bieutong12!m11:m100,B7)"
Range("g8").Value = "=COUNTIFS(bieutong12!m11:m100,B8)"
Range("g9").Value = "=COUNTIFS(bieutong12!m11:m100,B9)"

Range("h6").Value = "=COUNTIFS(bieutong12!o11:eek:100,B6)" 'Tieng dan toc
Range("h7").Value = "=COUNTIFS(bieutong12!o11:eek:100,B7)"
Range("h8").Value = "=COUNTIFS(bieutong12!o11:eek:100,B8)"
Range("h9").Value = "=COUNTIFS(bieutong12!o11:eek:100,B9)"

Range("i6").Value = "=COUNTIFS(bieutong12!q11:q100,B6)" 'Dao duc
Range("i7").Value = "=COUNTIFS(bieutong12!q11:q100,B7)"
Range("i8").Value = "=COUNTIFS(bieutong12!q11:q100,B8)"
Range("i9").Value = "=COUNTIFS(bieutong12!q11:q100,B9)"

Range("j6").Value = "=COUNTIFS(bieutong12!r11:r100,B6)" 'Am nhac
Range("j7").Value = "=COUNTIFS(bieutong12!r11:r100,B7)"
Range("j8").Value = "=COUNTIFS(bieutong12!r11:r100,B8)"
Range("j9").Value = "=COUNTIFS(bieutong12!r11:r100,B9)"

Range("k6").Value = "=COUNTIFS(bieutong12!s11:s100,B6)" 'my thuat
Range("k7").Value = "=COUNTIFS(bieutong12!s11:s100,B7)"
Range("k8").Value = "=COUNTIFS(bieutong12!s11:s100,B8)"
Range("k9").Value = "=COUNTIFS(bieutong12!s11:s100,B9)"

Range("l6").Value = "=COUNTIFS(bieutong12!t11:t100,B6)" 'HDTN
Range("l7").Value = "=COUNTIFS(bieutong12!t11:t100,B7)"
Range("l8").Value = "=COUNTIFS(bieutong12!t11:t100,B8)"
Range("l9").Value = "=COUNTIFS(bieutong12!t11:t100,B9)"

Range("M6").Value = "=COUNTIFS(bieutong12!u11:u100,B6)" 'the chat
Range("M7").Value = "=COUNTIFS(bieutong12!u11:u100,B7)"
Range("M8").Value = "=COUNTIFS(bieutong12!u11:u100,B8)"
Range("M9").Value = "=COUNTIFS(bieutong12!u11:u100,B9)"

Range("c6:m9").Value = Range("c6:m9").Value 'chuyen toan bo sang value

'TONG HOP NANG LUC chung
Range("C15").Value = "=COUNTIFS(bieutong12!v11:v100,B15)" 'tu quan
Range("C16").Value = "=COUNTIFS(bieutong12!v11:v100,B16)"
Range("C17").Value = "=COUNTIFS(bieutong12!v11:v100,B17)"


Range("D15").Value = "=COUNTIFS(bieutong12!w11:w100,B15)" 'Giao tiep
Range("D16").Value = "=COUNTIFS(bieutong12!w11:w100,B16)"
Range("D17").Value = "=COUNTIFS(bieutong12!w11:w100,B17)"

Range("E15").Value = "=COUNTIFS(bieutong12!x11:x100,B15)" 'Giai quyet van de
Range("E16").Value = "=COUNTIFS(bieutong12!x11:x100,B16)"
Range("E17").Value = "=COUNTIFS(bieutong12!x11:x100,B17)"

'nang luc dac thu
Range("F15").Value = "=COUNTIFS(bieutong12!y11:y100,B15)" 'Ngon ngu
Range("F16").Value = "=COUNTIFS(bieutong12!y11:y100,B16)"
Range("F17").Value = "=COUNTIFS(bieutong12!y11:y100,B17)"

Range("G15").Value = "=COUNTIFS(bieutong12!z11:z100,B15)" 'toan hoc
Range("G16").Value = "=COUNTIFS(bieutong12!z11:z100,B16)"
Range("G17").Value = "=COUNTIFS(bieutong12!z11:z100,B17)"

Range("H15").Value = "=COUNTIFS(bieutong12!Aa11:Aa100,B15)" 'tham my
Range("H16").Value = "=COUNTIFS(bieutong12!Aa11:Aa100,B16)"
Range("H17").Value = "=COUNTIFS(bieutong12!Aa11:Aa100,B17)"

Range("i15").Value = "=COUNTIFS(bieutong12!Ab11:Ab100,B15)" 'the chat
Range("i16").Value = "=COUNTIFS(bieutong12!Ab11:Ab100,B16)"
Range("i17").Value = "=COUNTIFS(bieutong12!Ab11:Ab100,B17)"

'Pham chat

Range("j15").Value = "=COUNTIFS(bieutong12!Ac11:Ac100,B15)" 'Yeu nuoc
Range("j16").Value = "=COUNTIFS(bieutong12!Ac11:Ac100,B16)"
Range("j17").Value = "=COUNTIFS(bieutong12!Ac11:Ac100,B17)"


Range("k15").Value = "=COUNTIFS(bieutong12!Ad11:Ad100,B15)" 'Nhan ai
Range("k16").Value = "=COUNTIFS(bieutong12!Ad11:Ad100,B16)"
Range("k17").Value = "=COUNTIFS(bieutong12!Ad11:Ad100,B17)"

Range("L15").Value = "=COUNTIFS(bieutong12!Ae11:Ae100,B15)" 'Cham chi
Range("L16").Value = "=COUNTIFS(bieutong12!Ae11:Ae100,B16)"
Range("L17").Value = "=COUNTIFS(bieutong12!Ae11:Ae100,B17)"

Range("M15").Value = "=COUNTIFS(bieutong12!Af11:Af100,B15)" 'Trung thuc
Range("M16").Value = "=COUNTIFS(bieutong12!Af11:Af100,B16)"
Range("M17").Value = "=COUNTIFS(bieutong12!Af11:Af100,B17)"

Range("N15").Value = "=COUNTIFS(bieutong12!Ag11:Ag100,B15)" 'trach nhiem
Range("N16").Value = "=COUNTIFS(bieutong12!Ag11:Ag100,B16)"
Range("N17").Value = "=COUNTIFS(bieutong12!Ag11:Ag100,B17)"


Range("C15:N17").Value = Range("C15:N17").Value
'Mo lai bao loi
On Error GoTo 0
Application.ScreenUpdating = True
End Sub
Tự thêm gia vị
Mã:
Sub ABC()
  Dim sArr(), aDK(), aCol, Res()
  Dim sRow&, sR&, sC&, i&, r&, j&, dk
 
  sArr = Sheets("bieutong12").Range("F11:U100").Value
  aDK = Range("B6:B9").Value
  aCol = Array("", 1, 3, 5, 6, 8, 10, 12, 13, 14, 15, 16)
  sRow = UBound(sArr)
  sR = UBound(aDK): sC = UBound(aCol)
  ReDim Res(1 To sR, 1 To sC)
  For i = 1 To sR
    dk = aDK(i, 1)
    If dk <> Empty Then
      For r = 1 To sRow
        For j = 1 To sC
          If sArr(r, aCol(j)) = dk Then Res(i, j) = Res(i, j) + 1
        Next j
      Next r
    End If
  Next i
  Range("C6:M9") = Res
 
  sArr = Sheets("bieutong12").Range("V11:AG100").Value
  aDK = Range("B15:B17").Value
  aCol = Array("", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
  sRow = UBound(sArr)
  sR = UBound(aDK): sC = UBound(aCol)
  ReDim Res(1 To sR, 1 To sC)
  For i = 1 To sR
    dk = aDK(i, 1)
    If dk <> Empty Then
      For r = 1 To sRow
        For j = 1 To sC
          If sArr(r, aCol(j)) = dk Then Res(i, j) = Res(i, j) + 1
        Next j
      Next r
    End If
  Next i
  Range("C15:N17") = Res
End Sub
 
Các anh (chị) cho em hỏi cách sử dụng hàm Countif trong VBA để đếm theo điều kiện trong vùng.
các anh/chị ơi cho em hỏi chút về cách sử dụng hàm COUNTIFS trong VBA với ạ.

trong Excel thì hàm này chọn được 2 miền trở lên :
COUNTIFS(criteria_range1, criteria1, [criteria_range2, criteria2],…)
criteria_range1: Vùng điều kiện thứ nhất
criteria1: Điều kiện để đếm thứ nhất
criteria_range2: Vùng điều kiện thứ hai
criteria2: Điều kiện để đếm thứ hai.

trong thư viện VBA thì hàm này không chọn được 2 miền trở lên.
mà lại chỉ chọn được 1 miền giống như COUNTIF:
Function CountIfs(Arg1 As Range, Arg2, [Arg3], [Arg4], [Arg5],) As Double

Nhờ các anh chị xem có phương án nào thay thế trong VBA không ạ ? em cần dùng 2 miền điều kiện để đếm như trong EXCEL ạ.
Em xin cảm ơn.
 
Web KT
Back
Top Bottom