範囲からバリアントへの変換で立ち往生しています。
percentAbove の機能については、0 の要素を削除してから、percentAboveHelper の入力を使用したいと考えています。たとえば、
xInput は 1、2、3、4、5、6、7、8、9、10 です
y 入力は 5、0、0、2、3、4、0、4、5、0 です
percentAboveHelper に渡される入力が次のようになることを望みます
x入力: 1、4、5、6、8、9
y入力: 5、2、3、4、4、5
percentAboveHelper の機能については、それ自体は正常に機能します。しかし、percentageAbove からバリアントを渡すと、#value が得られます。
#value! が原因の行を確認してみました。それで、msgbox "1" と msgbox "2" を percentAboveHelper に書き込みます。percentAboveHelper 自体のみを使用すると、メッセージ 1 と 2 が表示されます。しかし、percentageAbove を使用すると、メッセージ 1 のみが表示されます。
この投稿から、範囲から配列への切り替えと VBA 関数への戻りから、変換は variant = range.value で簡単に実行できることがわかります。しかし、私の場合はうまくいきません。何か提案はありますか?
Function percentageAbove(above As Double, x As Double, xInput As Excel.Range, yInput As Excel.Range)
Dim xRange As Excel.Range, yRange As Excel.Range
For index = 1 To xInput.count
If Not yInput.Item(index) = 0 Then
If Not yRange Is Nothing Then
Set xRange = Union(xRange, xInput.Item(index))
Set yRange = Union(yRange, yInput.Item(index))
Else
Set xRange = xInput.Item(index)
Set yRange = yInput.Item(index)
End If
End If
Next index
' I do check the xRange and yRange. Both contain elements
Dim xVariant As Variant, yVariant As Variant
xVariant = xRange.Value
yVariant = yRange.Value
percentageAbove = percentageAboveHelper(above, x, xVariant, yVariant)
End Function
Function percentageAboveHelper(above As Double, x As Double, xVariant As Variant, yVariant As Variant)
Dim n As Integer, df As Integer, meanOfX As Double, expectedY As Double, sste As Double, ssx As Double, se As Double
n = Application.count(xVariant)
df = n - 2
meanOfX = Application.Average(xVariant)
MsgBox "1"
expectedY = Application.Forecast(x, yVariant, xVariant)
MsgBox "2"
sste = Application.StEyx(yVariant, xVariant)
ssx = Application.DevSq(xVariant)
se = sste * Sqr(1 / n + (x - meanOfX) ^ 2 / ssx)
Dim tValue As Double, oneTailConf As Double
tValue = (expectedY - above) / se
oneTailConf = Application.TDist(Abs(tValue), df, 1)
If tValue > 0 Then
percentageAboveHelper = 1 - oneTailConf
Else
percentageAboveHelper = oneTailConf
End If
End Function