私はルーチン計算の一部を自動化する関数を記述しようとしています。
しかし、私が出会った、次のような問題があります。VBA関数は1つのワークシートでのみ動作します
SumbyCode1
関数は常にデータが含まれているシートで動作します。ただし、同じブックの他のワークシートでは機能しません。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
「With ws」を使用する場合、このシートに割り当てる必要があるすべての範囲オブジェクトは、その前に '.'を付ける必要があります。例: 'colnumbercode = Application.WorksheetFunction.Match(" Type "、.Range(" 1:1 ")、0)' –
長い行を投稿するときに行継続を使用することを検討してください。 )すべての水平スクロール... –
ありがとう、答えと提案をありがとう。それは今働きます! – czqstanley