2017-07-10 3 views
0

私はルーチン計算の一部を自動化する関数を記述しようとしています。
しかし、私が出会った、次のような問題があります。VBA関数は1つのワークシートでのみ動作します

  1. SumbyCode1関数は常にデータが含まれているシートで動作します。ただし、同じブックの他のワークシートでは機能しません。
  2. CountbyCode機能が動作しません。私は普通のサブとして機能を試して、それは完全にそこで動作します。しかし、私はコードを関数に適用します。それはまったく動作しません。以下

を参照してくださいコード:

Public Function SumbyCode1(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) 
    Dim var() 
    var = Array(wirecode0, wirecode1, wirecode2, wirecode3, wirecode4, _ 
       wirecode5, wirecode6, wirecode7, wirecode8) 

    Dim ws As Worksheet 
    Set ws = Worksheets("Banking Transaction") 
    Dim colnumbercode As Integer 
    Dim colnumberamount As Integer 
    Dim total As Variant 
    total = 0 

    With ws 
     colnumbercode = Application.WorksheetFunction.Match("Type", Range("1:1"), 0) 
     colnumbercodeletter = Chr(64 + colnumbercode) 
     codecol = colnumbercodeletter & ":" & colnumbercodeletter 
     colnumberamount = Application.WorksheetFunction.Match("Amount", Range("1:1"), 0) 
     colnumberamountletter = Chr(64 + colnumberamount) 
     codeamount = colnumberamountletter & ":" & colnumberamountletter 

     For i = 0 To 8 
      total = Application.WorksheetFunction.SumIf(Range(codecol), _ 
      var(i), Range(codeamount)) + total 
     Next i 
    End With 
    SumbyCode1 = total 

End Function 

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) 
    Dim var() 
    var = Array(wirecode0, wirecode1, wirecode2, wirecode3, _ 
       wirecode4, wirecode5, wirecode6, wirecode7, wirecode8) 

    Dim ws As Worksheet 
    Set ws = Worksheets("Banking Transaction") 
    Dim colnumbercode As Integer 
    Dim total As Variant 
    total = 0 

    With ws 
     colnumbercode = Application.WorksheetFunction.Match("Type", Range("1:1"), 0) 
     colnumbercodeletter = Chr(64 + colnumbercode) 
     codecol = colnumbercodeletter & ":" & colnumbercodeletter 

     For i = 0 To 8 
     total = Application.WorksheetFunction.CountIf(Range(codecol), _ 
       var(i)) + total 
     Next i 
    End With 
    CountbyCode = total 

End Function 
+2

「With ws」を使用する場合、このシートに割り当てる必要があるすべての範囲オブジェクトは、その前に '.'を付ける必要があります。例: 'colnumbercode = Application.WorksheetFunction.Match(" Type "、.Range(" 1:1 ")、0)' –

+3

長い行を投稿するときに行継続を使用することを検討してください。 )すべての水平スクロール... –

+0

ありがとう、答えと提案をありがとう。それは今働きます! – czqstanley

答えて

2

あなたは完全にあなたの参照を修飾する必要があります。あなたが使用する場合:

total = Application.WorksheetFunction.CountIf(Range(codecol), var(i)) + total

をVBエディタは黙っ機能のみを意味し、現在アクティブなシート上で動作ThisWorkbook.ActiveSheet.Range(codecol)、などRange(codecol)解釈します。 @ScottCranerが示唆しているように、Range(codecol).Range(codecol)に変更して、前のWith ws宣言を使用して完全に明示的な参照に変更する必要があります。

+2

Rubberduck VBEアドインのユーザーは、Rubberduckのコード検査が将来、このエラー(およびそのような多くの他のもの)をキャッチして自動的に修正できることを理解できます。 http://rubberduckvba.com/ – puzzlepiece87

+0

ありがとうございます!それは今働く。あなたが言及した参考文献を読んで、さらに改善する予定です。再度、感謝します! – czqstanley

関連する問題