将Countif转换为VBA

时间:2016-04-30 18:56:54

标签: excel vba excel-vba

我已将以下功能编写到我的(Excel 2010)工作簿中:

    Function Ifs4Count(r As Range, v As String, r2 As Range, v2 As String)
    Ifs4Count = Application.WorksheetFunction.CountIfs(r, v, r2, v2)
    End Function

    Function Ifs6Count(r As Range, v As String, r2 As Range, v2 As String, r3 As Range, v3 As String)
    Ifs6Count = Application.WorksheetFunction.CountIfs(r, v, r2, v2, r3, v3)
    End Function

    Function Ifs8Count(r As Range, v As String, r2 As Range, v2 As String, r3 As Range, v3 As String, r4 As Range, v4 As String)
    Ifs8Count = Application.WorksheetFunction.CountIfs(r, v, r2, v2, r3, v3, r4, v4)
    End Function

    Function ifcount(r As Range, v As String)
    ifcount = Application.WorksheetFunction.CountIf(r, v)
    End Function

这些用于各种subs,它们根据选中的复选框返回值,例如:

If RatchetSizeComboBox.Enabled = False And EndCap2ComboBox.Enabled = False Then
            NumberOOC2TextBox.value = Ifs4Count(rCOMMENTS, "SEND TO CAL", _
                rBRAND, BrandComboBox.value) 'Only Brand
        ElseIf RatchetSizeComboBox.Enabled = False And EndCap2ComboBox.Enabled = True Then
            NumberOOC2TextBox.value = Ifs6Count(rCOMMENTS, "SEND TO CAL", _
                rBRAND, BrandComboBox.value, _
                rCAP, EndCap2ComboBox.value) 'Brand and End Cap
        ElseIf RatchetSizeComboBox.Enabled = True And EndCap2ComboBox.Enabled = False Then
            NumberOOC2TextBox.value = Ifs6Count(rCOMMENTS, "SEND TO CAL", _
                rBRAND, BrandComboBox.value, _
                rRATCHET, RatchetSizeComboBox.value) 'Brand and Size
        Else
            NumberOOC2TextBox.value = Ifs8Count(rCOMMENTS, "SEND TO CAL", _
                rBRAND, BrandComboBox.value, _
                rRATCHET, RatchetSizeComboBox.value, _
                rCAP, EndCap2ComboBox.value) 'Brand, Size and End Cap
        End If

我认为必须有一种方法来使用for循环,所以每次我需要使用不同数量的变量进行ifcount时我不必使用所有这些不同的函数,但我不是很有经验有了VBA,所以我希望能得到一些帮助。我怎么能转换这个,所以我没有使用工作表函数,而是实际的VBA?任何帮助表示赞赏。

在尝试了一些测试代码之后,我认为我已经拥有它,但现在有了:

Function IfsCount(r As Range, v As String, Optional r2 As Range, Optional v2 As String, Optional r3 As Range, Optional v3 As String, Optional r4 As Range, Optional v4 As String)
    IfsCount = Application.WorksheetFunction.CountIfs(r, v, [r2], [v2], [r3], [v3], [r4], [v4])
End Function

我收到了无效的电话或程序。括号不是标记为可选的正确方法吗?

最终代码略微修改了Dirk的答案(如果它超过v2则没有评估每个变量)。我似乎无法在此网站上添加代码,所以基本上我所做的是为每个“If Len”语句创建一个新的str变量,然后将评估更改为:

IfsCount = Evaluate("COUNTIFS('" & r.Parent.Name & "'!" & r.Address & ",""" & v & """" & str & str2 & str3 & ")")

感谢大家的建议!

3 个答案:

答案 0 :(得分:1)

你在尝试什么,是不可能的!它只是 vba-syntax ,它不支持类似的东西......

同样具有非变体的可选参数,不可能检测它们是否被省略。做你正在尝试的事情的唯一方法可能是评估功能:

Function IfsCount(r As Range, v As String, Optional r2 As Range, Optional v2 As String, Optional r3 As Range, Optional v3 As String, Optional r4 As Range, Optional v4 As String)
  Dim str As String
  If Len(v2) Then str = ",'" & r2.Parent.Name & "'!" & r2.Address & ",""" & v2 & """"
  If Len(v3) Then str = ",'" & r3.Parent.Name & "'!" & r3.Address & ",""" & v3 & """"
  If Len(v4) Then str = ",'" & r4.Parent.Name & "'!" & r4.Address & ",""" & v4 & """"
  IfsCount = Evaluate("COUNTIFS('" & r.Parent.Name & "'!" & r.Address & ",""" & v & """" & str & ")")
End Function

答案 1 :(得分:0)

If RatchetSizeComboBox.Enabled = False And EndCap2ComboBox.Enabled = False Then
    NumberOOC2TextBox.value = Application.WorksheetFunction.CountIfs(rCOMMENTS, "SEND TO CAL", rBRAND, BrandComboBox.value) 'Only Brand
ElseIf RatchetSizeComboBox.Enabled = False And EndCap2ComboBox.Enabled = True Then
    NumberOOC2TextBox.value = Application.WorksheetFunction.CountIfs(rCOMMENTS, "SEND TO CAL", rBRAND, BrandComboBox.value, rCAP, EndCap2ComboBox.value) 'Brand and End Cap
ElseIf RatchetSizeComboBox.Enabled = True And EndCap2ComboBox.Enabled = False Then
    NumberOOC2TextBox.value = Application.WorksheetFunction.CountIfs(rCOMMENTS, "SEND TO CAL", rBRAND, BrandComboBox.value, rRATCHET, RatchetSizeComboBox.value) 'Brand and Size
Else
    NumberOOC2TextBox.value = Application.WorksheetFunction.CountIfs(rCOMMENTS, "SEND TO CAL", rBRAND, BrandComboBox.value, rRATCHET, RatchetSizeComboBox.value, rCAP, EndCap2ComboBox.value) 'Brand, Size and End Cap
End If

跳过整个用户功能,直接从if语句的正文中调用。您还应该声明您正在使用的工作表我相信。

答案 2 :(得分:0)

你可以这样做

Dim sRATCHET As String, sCAP As String

sRATCHET = IIf(RatchetSizeComboBox.Enabled, RatchetSizeComboBox.Value, "*")
sCAP = IIf(EndCap2ComboBox.Enabled, EndCap2ComboBox.Value, "*")

NumberOOC2TextBox.Value = Application.WorksheetFunction.CountIfs(rCOMMENTS, "SEND TO CAL", _
                                                                  rBRAND, BrandComboBox.Value, _
                                                                  rRATCHET, sRATCHET, _
                                                                  rCAP, sCAP)