比较VBA中的计数

时间:2017-01-10 05:44:53

标签: vba excel-vba excel

我有以下代码,可根据特定搜索条件获取值的计数:

Sub WBR()
    Dim wf As WorksheetFunction
    Set wf = Application.WorksheetFunction

    With ActiveWorkbook.Worksheets("TT") 'no of tickets processed - summary
        [AE43] = wf.CountIfs(.Range("I:I"), "<>Duplicate TT", _
            .Range("G:G"), "<>Not Tested", _
            .Range("U:U"), "Item")
    End With

    With ActiveWorkbook.Worksheets("TT") 'not tested tickets - summary
        [AE44] = wf.CountIfs(.Range("G:G"), "Not Tested")
    End With

    With ActiveWorkbook.Worksheets("TT") 'Tickets moved back- outdated OS and App Versions - summary
        [AE45] = wf.CountIf(.Range("I:I"), "Outdated App Version") + wf.CountIf(.Range("I:I"), "Outdated OS")
    End With
End Sub

现在我需要做一个类似的功能,但不知道该怎么做:

  • 检查列FG中的COMPATIBLE
  • 计数
  • 如果F的{​​{1}}更多,则应在单元格中更新该值
  • 否则,如果COMPATIBLE有更多计数,则应在同一单元格中更新。

如何使用上面的代码执行此操作?

1 个答案:

答案 0 :(得分:2)

您可以在现有代码的末尾添加此内容。只需在COMPATIBLEF列中计算G,并根据您的条件设置[AE46]的值:

' compare columns F and G compatible counts
Dim lngFCompatibleCount As Long
Dim lngGCompatibleCount As Long

With ActiveWorkbook.Worksheets("TT")
    lngFCompatibleCount = wf.CountIf(.Range("F:F"), "COMPATIBLE")
    lngGCompatibleCount = wf.CountIf(.Range("G:G"), "COMPATIBLE")
End With

If lngFCompatibleCount > lngGCompatibleCount Then
    [AE46] = lngFCompatibleCount
Else 'not sure about condition where count for F= count for G
    [AE46] = lngGCompatibleCount
End If