2017-07-13 18 views
0

で動作します。トランザクションコードで銀行取引をカウントする関数を記述しています。ただし、countif関数はVBA関数では機能しません。それでも、私が独立したサブと同じコードのセットを実行すると、関数は機能し、正しい値を返します。CountIf関数はVBA関数では正しく機能しませんが、サブ関数

私は数式を回避するさまざまな方法を試みてきましたが、CountIfがここではうまくいかない理由は非常に混乱しています。

ありがとうございました。あなたがwirecode1wirecode2(など)の値をお勧めしていない場合は

  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 
+0

動作するのではなく、何をしますか? –

答えて

0

、彼らはMissingの値が代入されます(注:ない"Missing") は、以下のコードです。

Missing""を比較する(またはそれ以外の場合はStringとして扱う)ことはできません。エラーは発生しません。

私はあなたが欠落している値をテストするためにあなたのループを変更するお勧めします。

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)を使用することができます。

+0

ありがとう!それは最終的に動作し、提案にも感謝! – czqstanley

関連する問題