使用VBA Macro计算所有属于美国的员工

时间:2015-07-14 08:59:26

标签: excel-vba vba excel

考虑一张包含100名员工的表格,其中包括美国,加拿大,英国,丹麦等。需要获得VBA宏来统计所有属于美国,加拿大,英国,丹麦等的员工。

我可以使用CountIfs通过公式完成它,但不能通过VBA宏完成。

3 个答案:

答案 0 :(得分:1)

在这里,我的方法适合你。试试这个。

Sheet2.Range("C5") = WorksheetFunction.CountIfs(Sheet2.Range("B3:B23"), "USA", Sheet2.Range("H3:H23"), "A")

答案 1 :(得分:0)

13帖子,我不明白你怎么回事,我不回答为什么VBA,为什么不擅长公式o枢轴 您可以使用明确的标准或动态地在您想要的地方使用简单的公式 enter image description here

答案 2 :(得分:0)

    Sub Test()
Dim Masterworksht As Worksheet
Dim Oworksht As Worksheet
Dim intRow, intMRow, intH, intNor, intL, intClosed As Integer

Set Masterworksht = ThisWorkbook.Worksheets("Master")
Set Oworksht = ThisWorkbook.Worksheets("Output")
intRow = 4

Do
    intH = 0
    intNor = 0
    intL = 0
    intClosed = 0
    intMRow = 3
    Do
        If (Oworksht.Cells(intRow, 1).Value = Masterworksht.Cells(intMRow, 2).Value) Then
            If (Masterworksht.Cells(intMRow, 8).Value = "High") Then
                intH = intH + 1
            ElseIf (Masterworksht.Cells(intMRow, 8).Value = "Low") Then
                intL = intL + 1
            ElseIf (Masterworksht.Cells(intMRow, 8).Value = "Normal") Then
                intNor = intNor + 1
            End If
            If (Masterworksht.Cells(intMRow, 12).Value = "Closed") Then
                intClosed = intClosed + 1
            End If
        End If
        intMRow = intMRow + 1
    Loop While Masterworksht.Cells(intMRow, 1).Value <> ""
    Oworksht.Cells(intRow, 2).Value = intH
    Oworksht.Cells(intRow, 3).Value = intNor
    Oworksht.Cells(intRow, 4).Value = intL
    Oworksht.Cells(intRow, 6).Value = intClosed
    intRow = intRow + 1
Loop While Oworksht.Cells(intRow, 1).Value <> ""

End Sub