1

データの範囲を取り、ユーザーが提供した列から 2 つの数値の比率を計算する関数を作成しようとしています。この比率を行の最後に出力したいのですが、何らかの理由で、セル関数を使用して行の最後のセルを参照できません。代わりに、Cells 関数は、セル アドレスではなく、そのセルの値を提供し続けます。セル関数もアドレスを提供すると思いました。これが間違っているのか、それとも私のコードが間違っているのか、誰かに教えてもらえますか?

ここにコードがあります

Function calculateRatio(table As Range, numerator As Integer, denominator As Integer,       Optional nameOfRatio As String)
On Error GoTo ExpectedError
    Dim num As Double
    Dim denom As Double
    Dim ratio As Double

    If table.Columns.Count < 2 Then
        MsgBox ("Not enough data. Requires at least two or more rows.")
        Exit Function
    End If
    If numerator < 1 Or numerator > table.Columns.Count Then
        MsgBox ("Not an acceptable Numerator. Must be greater than zero and less than " & table.Columns.Count)
        Exit Function
    End If
    If denominator < 1 Or denominator > table.Columns.Count Then
        MsgBox ("Not an acceptable Denominator. Must be greater than zero and less than " & table.Columns.Count)
     Exit Function
     End If
    For Counter = 1 To table.Rows.Count
        num = table.cells(Counter, numerator)
        denom = table.cells(Counter, denominator)
        ratio = num / denom
        temp = table.cells(counter, table.columns.count)
        temp.Offset(0, 1).Value = ratio
     Next Counter

 Exit Function
ExpectedError:
    Call MsgBox("Something went wrong. Make sure you are referencing columns with numbers and not text." & Err.Number & " : " & Err.Description)
End
End Function

アップデート

更新されたコードは次のとおりです。

Function calculateRatio(table As Range, numerator As Integer, denominator As Integer, Optional nameOfRatio As String)
    Dim num As Double
   Dim denom As Double
   Dim ratio As Double
   Dim temp As Range
   Dim counter As Integer

    If table.Columns.Count < 2 Then
        MsgBox ("Not enough data. Requires at least two or more rows.")
        Exit Function
    End If
    If numerator < 1 Or numerator > table.Columns.Count Then
        MsgBox ("Not an acceptable Numerator. Must be greater than zero and less than " & table.Columns.Count)
        Exit Function
    End If
    If denominator < 1 Or denominator > table.Columns.Count Then
         MsgBox ("Not an acceptable Denominator. Must be greater than zero and less than " & table.Columns.Count)
        Exit Function
    End If
    For counter = 1 To table.Rows.Count
        num = table.cells(counter, numerator)
        denom = table.cells(counter, denominator)
        ratio = num / denom
        Set temp = table.cells(counter, table.Columns.Count)
        temp.Offset(0, 1).Value = ratio
    Next counter
End Function
4

3 に答える 3

1

追加Dim temp as Rangeして変更temp = table.cells(counter, table.columns.count)してみてくださいset temp = table.cells(counter, table.columns.count)

本当にあなたがする必要があるのは、を追加するsetことだけです。VariantlongVariantRange

于 2013-03-27T22:54:47.067 に答える
1

ワークシートから呼び出された UDF からワークシート セルを操作することはできません。

詳細はこちら:

https://stackoverflow.com/a/15647054/1467082

そしてここ:

http://www.excel-it.com/UDF.htm

通常、サブルーチンはワークシートを操作できますが、関数は操作できません。

例外として、サブルーチン内から呼び出される関数は使用できますが、サブルーチンに値を返す以外の目的で関数を使用するのは、おそらく悪い習慣です。

于 2013-03-27T23:47:28.547 に答える
0

コードで使用していないOption Explicitと思います。

「temp」を範囲として宣言する必要があります。

Dim temp As Range ' somewhere at the top of your function

For Counter = 1 To table.Rows.Count
......
Set temp = table.cells(Counter, table.columns.count)
...
Next

セル座標を取得している場合は、手動でオフセットしてみませんか?

table.cells(counter, table.Columns.Count+1).Value = ratio

これを試して:

Function calculateRatio(table As Range, numerator As Integer, denominator As Integer, Optional nameOfRatio As String)
    Dim num As Double
   Dim denom As Double
   Dim ratio As Double
   Dim temp As Range
   Dim counter As Integer

    If table.Columns.Count < 2 Then
        MsgBox ("Not enough data. Requires at least two or more rows.")
        Exit Function
    End If
    If numerator < 1 Or numerator > table.Columns.Count Then
        MsgBox ("Not an acceptable Numerator. Must be greater than zero and less than " & table.Columns.Count)
        Exit Function
    End If
    If denominator < 1 Or denominator > table.Columns.Count Then
         MsgBox ("Not an acceptable Denominator. Must be greater than zero and less than " & table.Columns.Count)
        Exit Function
    End If
    For counter = 1 To table.Rows.Count
        num = table.cells(counter, numerator)
        denom = table.cells(counter, denominator)
        ratio = num / denom
        table.cells(counter, table.Columns(table.Columns.Count).Column + 1).Value = ratio
        'Set temp = table.cells(counter, table.Columns.Count)
        'temp.Offset(0, 1).Value = ratio
    Next counter
End Function

`table.columns(table.columns.count).column を使用すると、正しい列を参照していることを確認できます。現時点では問題を引き起こす可能性がありますが、安全である方がよい例は考えられません。

于 2013-03-27T22:55:26.540 に答える