传递参数并在vba

时间:2017-01-05 07:38:41

标签: excel vba excel-vba

我有以下代码,其中基于特定列,特定文本和特定工作表的搜索更新单元格的计数。

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

如何传递参数,使其满足条件时,应在指定的单元格中更新计数?

1 个答案:

答案 0 :(得分:0)

使用数组存储所有常量的

示例代码:

Sub WBR()
    Dim Count1Criteria As Variant
    Dim Count3Criteria As Variant
    Dim test As Variant
    Dim wf As WorksheetFunction
    Set wf = Application.WorksheetFunction

    Count1Criteria = Array(Array("AE44", "TT", "G:G", "Not Tested"), _
                           Array("AF44", "TT", "U:U", "Item"))

    Count3Criteria = Array(Array("AE43", "TT", "I:I", "<>Duplicate TT", _
                                               "G:G", "<>Not Tested", _
                                               "U:U", "Item"), _
                           Array("AF43", "TT", "I:I", "<>Duplicate TT", _
                                               "G:G", "<>Not Tested", _
                                               "U:U", "Item"))


    For Each test In Count1Criteria
        With Worksheets(test(1))
            Range(test(0)) = wf.CountIfs(.Range(test(2)), test(3))
        End With
    Next                                   
    For Each test In Count3Criteria
        With Worksheets(test(1))
            Range(test(0)) = wf.CountIfs(.Range(test(2)), test(3), _
                                         .Range(test(4)), test(5), _
                                         .Range(test(6)), test(7))
        End With
    Next                                   
End Sub

这不会处理你所有的情况 - 你有一些你需要在一起添加两个(或更多)结果的地方 - 但它应该让你知道如何减少代码的重复性和重复性#34 ;