Excel CountIf函数没有';在VBA函数中不能正常工作,但在sub中可以正常工作

Excel CountIf函数没有';在VBA函数中不能正常工作,但在sub中可以正常工作,excel,vba,Excel,Vba,我一直在写一个函数,通过交易代码统计银行交易。但是,countif函数在VBA函数中不起作用。然而,当我作为一个独立的子函数运行同一组代码时,该函数工作并返回正确的值 我一直在尝试不同的方法来解决这个公式,但我仍然很困惑为什么CountIf在这里不起作用 多谢各位。 代码如下: Sub Test() ' Input Transaction Code wirecode0 = InputBox("code1")

我一直在写一个函数,通过交易代码统计银行交易。但是,countif函数在VBA函数中不起作用。然而,当我作为一个独立的子函数运行同一组代码时,该函数工作并返回正确的值

我一直在尝试不同的方法来解决这个公式,但我仍然很困惑为什么CountIf在这里不起作用

多谢各位。 代码如下:

            Sub Test()

            ' Input Transaction Code

                wirecode0 = InputBox("code1")
                wirecode1 = InputBox("code2")

            ' Pass codes to array
            Dim var()
                var = Array(wirecode0, wirecode1, wirecode2, wirecode3, _
                wirecode4, wirecode5, wirecode6, wirecode7, wirecode8)

            ' Define worksheet and variables
            Dim ws As Worksheet
            Set ws = Worksheets("Banking Transaction")
            Dim colnumbercode As Integer
            Dim totalcount As Integer

            'Locate the column "Type" which contains the transaction codes
                    With ws
                    colnumbercode = Application.WorksheetFunction.Match("Type", .Range("1:1"), 0)
                    colnumbercodeletter = Chr(64 + colnumbercode)
                    codecol = colnumbercodeletter & ":" & colnumbercodeletter

            'Count codes
                            For i = 0 To 8
                                If var(i) = "" Then
                                var(i) = 0
                                End If
                            totalcount = Application.WorksheetFunction.CountIf(.Range(codecol), var(i)) + totalcount
                            Next i
                    End With
                MsgBox (totalcount)
            End Sub


            Public Function CountbyCode(ByRef wirecode0, Optional ByRef wirecode1, _
                                        Optional ByRef wirecode2, Optional ByRef wirecode3, _
                                        Optional ByRef wirecode4, Optional ByRef wirecode5, _
                                        Optional ByRef wirecode6, Optional ByRef wirecode7, _
                                        Optional ByRef wirecode8)
            ' Pass codes to array
            Dim var()
                                        var = Array(wirecode0, wirecode1, wirecode2, wirecode3, _
                                        wirecode4, wirecode5, wirecode6, wirecode7, wirecode8)

            ' Define worksheet and variables
            Dim ws As Worksheet
            Set ws = Worksheets("Banking Transaction")
            Dim colnumbercode As Integer
            Dim totalcount As Integer

            'Locate the column "Type" which contains the transaction codes
                    With ws
                    colnumbercode = Application.WorksheetFunction.Match("Type", .Range("1:1"), 0)
                    colnumbercodeletter = Chr(64 + colnumbercode)
                    codecol = colnumbercodeletter & ":" & colnumbercodeletter

             'Count codes
                            For i = 0 To 8
                                If var(i) = "" Then
                                var(i) = 0
                                End If
                                totalcount = Application.WorksheetFunction.CountIf(.Range(codecol), var(i)) + totalcount
                            Next i
                    End With

            CountbyCode = totalcount

            End Function

如果您没有为
wirecode1
wirecode2
(等)建议一个值,它们将被分配一个
缺失的值(注意:不是
“缺失”

在不生成错误的情况下,无法将
缺失的
”(或将其视为
字符串)进行比较

我建议您更改循环以测试缺少的值:

For i = 0 To 8
    If Not IsMissing(var(i)) Then
        If var(i) = "" Then
            var(i) = 0
        End If
        totalcount = Application.WorksheetFunction.CountIf(.Range(codecol), var(i)) + totalcount
    End If
Next i


另外请注意,您不需要计算
codecol
——您可以使用
.Columns(colnumbercode)
而不是
.Range(codecol)

它做什么而不工作?谢谢!它终于起作用了,也谢谢你的建议!