私は特定のセルの値に応じて、テキストとして数式を返す検索を持っています。
返される式(テキスト)は次のようになります。
SUM(INDIRECT("AF"&row()),INDIRECT("AG"&row()))
次に、式を評価するために EVALUATE()
関数を使用しますが、 INDIRECT()
を使用するため#REFエラーが発生します。
This Microsoft Support Page was the only reference that I could find to this particular problem, and it doesn't seem to offer an appropriate workaround.
a)間接
の使用を避けるために数式を再構成するか、b)素敵な演奏のために EVALUATE()
を取得するにはどうすればよいですか?
編集
ルックアップテーブルは次のようになります。
Type A SUM(INDIRECT("AF"&row()),INDIRECT("AG"&row()))
Type B SUM(INDIRECT("AF"&row()),INDIRECT("AG"&row()), INDIRECT("AH"&Row()))
別のシートでは、セルB1:200の内容はタイプAまたはタイプBのいずれかです。ルックアップはセルの値に基づいて数式文字列を返し、Bxに配置します。私は結果を与えるために文字列を評価します。
OK 3回目の回答:
例のおかげで:これは、あなたが名前付き数式を評価するためにINDIRECTを使用しようとしており、INDIRECTが数式ではなく参照のみを処理しようとしているために失敗します。
You need to use EVALUATE instead, but there is no built-in EVALUATE worksheet function (the EVALUATE you are using in the defined name is an ancient XLM Macro function).
I suggest you use my EVAL VBA UDF instead
Public Function EVAL(theInput As Variant) As Variant
'
' if UDF evaluate the input string as though it was on this sheet
' else evaluate for activesheet
'
Dim vEval As Variant
Application.Volatile
On Error GoTo funcfail
If not IsEmpty(theInput) then
If TypeOf Application.Caller.Parent Is Worksheet Then
vEval = Application.Caller.Parent.Evaluate(Cstr(theInput))
Else
vEval = Application.Evaluate(cstr(theInput))
End If
If IsError(vEval) Then
EVAL = CVErr(xlErrValue)
Else
EVAL = vEval
End If
End If
Exit Function
funcfail:
EVAL = CVErr(xlErrNA)
End Function
typeA = SUM(Sheet3!RC1、Sheet3RC2)のような相対参照で定義された名前を使用します。
There are some "quirks" of EVALUATE that you should be aware of: see
http://www.decisionmodels.com/calcsecretsh.htm
If I understand what you are trying to do correctly, why not get the resukt directly from the cell ..
I don't think you need indirect because you just need the text of the formula
(altho I am not sure what the ROW() is supposed to be doing)
SUM("AF"&row(),"AG"&row())
If you just want to use a Defined Name to add column AF and AG on the current row then just use relative references (easier to define in R1C1 mode, then switch back to A1)
Defined Name AndySum has Refersto formula of =SUM(RC32,RC33)
Then wherever you use AndySum in a formula it will add the contents of columns AF and AG on that row.
Charlesの答えを拡張するには:この関数は検索を行い、行番号を評価します。私は数式(あなたの例では)最終的な結果を返すセルと同じ行から特定の値を合計することを意図していると仮定しています。
私のルックアップテーブルは次のようになります:
TypeA SUM(B ,D)
TypeB SUM(C,D)
UDF:
Public Function GetAndRunCalc(CalcType As String)
Application.Volatile
Dim ac As Object, rw, f
On Error GoTo haveError
If TypeOf Application.Caller.Parent Is Worksheet Then
Set ac = Application.Caller
rw = ac.Parent.Range(ac.Address).Row
'adjust for your lookup table...
f = Application.VLookup(CalcType, Sheet1.Range("B4:C5"), 2, False)
If Not IsError(f) Then
f = Replace(f, "", rw)
GetAndRunCalc = ac.Parent.Evaluate(f)
Else
GetAndRunCalc = "Type??"
End If
Else
GetAndRunCalc = "Must be called from worksheet cell"
End If
Exit Function
haveError:
GetAndRunCalc = CVErr(xlErrValue)
End Function