简体   繁体   English

Excel vba:在从该单元格调用的UDF执行之前,是否可以访问单元格的值?

[英]Excel vba: Is it possible to access the value of a cell before the UDF called from that cell executes?

I have a UDF which reads data from other sheets indirectly, adding the same cell across a number of sheets, ie: 我有一个UDF,它间接从其他工作表中读取数据,在多个工作表中添加相同的单元格,即:

Function myFunction( StrArgs As String ) As Long

  ....

End Function

I'm calling this function from cell A1, which right now has a value of 100: 我从单元格A1调用此函数,它现在的值为100:

=myFunction( ... )

Calculation is set to manual, and the sheet is refreshed when needed. 计算设置为手动,并在需要时刷新工作表。

Since the arguments StrArgs define the sheets to be queried, I'm including some error-checking inside myFunction in case either the specified sheets don't exist, or there's an error in the specification of StrArgs. 由于参数StrArgs定义了要查询的工作表,因此我在myFunction中包含一些错误检查,以防指定的工作表不存在,或者StrArgs的规范中存在错误。

This works fine, however what I'm struggling with is the following: when an error is found within myFunction, I want to return (keep) the existing value of the calling cell, instead of a zero or error value. 这很好,但是我正在努力解决的问题如下:当在myFunction中发现错误时,我想返回(保留)调用单元的现有值,而不是零或错误值。

What I want to do at the beginning of myFunction is: 我在myFunction开头想做的是:

existingCellValue = Application.Caller.Text   'or Application.Caller.Value

Then perform the calculations, and when an error is encountered: 然后执行计算,并在遇到错误时:

myFunction = existingCellValue

However, I find that this returns zero. 但是,我发现这会返回零。 In the debugger I see that, as soon as myFunction starts to execute, the cell value is already set to zero. 在调试器中,我看到,只要myFunction开始执行,单元格值就已经设置为零。

My question is - is there a way to access the existing value of the calling cell, before executing the UDF ? 我的问题是 - 在执行UDF之前,有没有办法访问调用单元的现有值?

-- edit - more complete code, as an example, which seems to work fine: -- - 编辑 - 更完整的代码,作为一个例子,似乎工作正常: -

Function GETNUMBER(Col As String, Row As Integer) As Double
    Dim LookStr As String
    Dim TheAnswer As Double
    Dim CellVal As Variant

On Error GoTo errHandler

    CellVal = Application.Caller.Text

    LookStr = "=" & Col & Row
    TheAnswer = Application.Evaluate(LookStr)

    GETNUMBER = TheAnswer

    On Error GoTo 0

Exit Function

errHandler:
    GETNUMBER = CellVal

End Function

With the above code in a module, I enter the following in my workbook: 使用模块中的上述代码,我在工作簿中输入以下内容:

Row
1:   | D    | 1    | =GETNUMBER(A1,B1)    | 10
2:   | D    | 2    | =GETNUMBER(A2,B2)    | 20
3:   | D    | 3    | =GETNUMBER(A3,B3)    | 30

This returns the values of 10, 20 and 30 from column D. 这将从D列返回值10,20和30。

Now I change one of the cells in column B to zero, to invoke the errHandler, and return CellVal, stored at the start. 现在我将B列中的一个单元格更改为零,以调用errHandler,并返回存储在开头的CellVal。

This seems to work, and both Application.Caller.Text and Application.ThisCell.Text give the correct result. 这似乎有效,Application.Caller.Text和Application.ThisCell.Text都给出了正确的结果。

Thanks to both Charles Watson and KazJaw, both of whom answered the question. 感谢Charles Watson和KazJaw,他们都回答了这个问题。

There are several possible ways, but they all have disadvantages. 有几种可能的方法,但它们都有缺点。
The simplest way is to use Application.Caller.Text but it returns the formatted value rather than the actual value. 最简单的方法是使用Application.Caller.Text但它返回格式化的值而不是实际值。
See my blog post on the subject for more discussion http://fastexcel.wordpress.com/2012/01/08/writing-efficient-vba-udfs-part-8-getting-the-previously-calculated-value-from-the-calling-cells/ 有关此主题的更多信息,请参阅我的博客文章http://fastexcel.wordpress.com/2012/01/08/writing-efficient-vba-udfs-part-8-getting-the-previously-calculated-value-from-该呼细胞/

You can do it with Application.ThisCell property . 您可以使用Application.ThisCell property执行此操作。 See quite simple example below: 看下面很简单的例子:

Public Function MultipleAB(a, b)
    Debug.Print Application.ThisCell.Text   'here you get current value
    MultipleAB = a * b                      'here you get new value
End Function

Important!! 重要!! Application.ThisCell is valid only for UDFs used in cells. Application.ThisCell仅对单元格中使用的UDF有效。 When used in other VBA Subs can return errors. 在其他VBA Subs中使用时可以返回错误。

The picture below presents how this solution works (for randomly changed values in cells C1 & C2): 下图显示了此解决方案的工作原理(对于单元格C1和C2中随机更改的值):

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM