Retrieving Sheets/Worksheets objects without making the function volatile

时间:2018-06-04 16:42:48

标签: excel vba excel-vba volatile

I'm trying to make a non volatile function that accepts an index and two ranges and makes certain operation on the sheet's ranges.

Unfortunately the Sheets or Worksheets functions make the function volatile thus each time I edit the worksheet the function starts recalculation.

Here's some pseudocode:

Function FooFunc(sheetIndex As Integer, xRange As Variant, yRange As Variant) As Variant    

   Dim operatingSheet As Variant

   'This makes the function volatile
   Set operatingSheet = Sheets("Bar " & sheetIndex)

   [...]

End Function

Apparently declaring Application.Volatile (False) doesn't seem to change anything.

As per title: is there a way to retrieve Sheets/Worksheets object without making the function volatile?

1 个答案:

答案 0 :(得分:0)

我无法重现您从VBA UDF访问工作表导致UDF易变的观察结果(Excel 2013)。

简单UDF:

Application.Volatile

enter image description here

在编辑输入范围A2和A3时,我只看到来自此UDF的调试输出:同一工作表上的其他编辑或计算不会触发输出。

我能看到你描述的内容的唯一方法是将{{1}}添加到UDF。