で動作します。トランザクションコードで銀行取引をカウントする関数を記述しています。ただし、countif関数はVBA関数では機能しません。それでも、私が独立したサブと同じコードのセットを実行すると、関数は機能し、正しい値を返します。CountIf関数はVBA関数では正しく機能しませんが、サブ関数
私は数式を回避するさまざまな方法を試みてきましたが、CountIfがここではうまくいかない理由は非常に混乱しています。
ありがとうございました。あなたがwirecode1
、wirecode2
(など)の値をお勧めしていない場合は
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
動作するのではなく、何をしますか? –