VBA为函数内的单元格赋值?

无论如何,我可以在一个函数内为单元格赋值/名称?

谢谢

编辑

对不起,不清楚,这是我的要求。

我有一个用户定义的函数(= MyFunction()),可以从Excel表中调用。 因此,我还有一个菜单button,当用户点击一个button时,我需要调用所有调用= MyFunction()的函数。

我的计划是在MyFunction()中,为调用单元分配一个名称rerefence并将其存储在vba中。 所以我可以有一个单元格的名称数组。 然后当菜单button被点击时,我可以调用这些单元格引用。

请帮我做到这一点。 有没有更好的方法来保持单元格引用?

编辑:

啊,现在我明白了。 最简单的方法是创build一个伪造的参数: MyFunction(ByVal r As Variant) ,并且,无论何时在表单上使用此函数,都提供与参数完全相同的单元格: =MyFunction(A1) 。 单击菜单项时,将A1中的值更改为任何值,并重新计算所有MyFunctions。

或者,您可以在函数的主体中使用Application.Volatile 。 这样,每次打开的工作簿中的任何单元格都被更改时,它将重新计算。

可以使用模块级别的集合来存储引用,但是Excel有时会重置该项目,从而丢失模块级别的variables。 如果你勇于尝试:

 Option Explicit Private RefsToCalculate As New Collection Public Function MyFunction() As Long Static i As Long i = i + 1 MyFunction = i If TypeOf Application.Caller Is Excel.Range Then On Error Resume Next RefsToCalculate.Add Application.Caller, Application.Caller.Address On Error GoTo 0 End If End Function Public Sub MenuButtonClicked() Dim i As Long For i = 1 To RefsToCalculate.Count RefsToCalculate(i).Dirty Next End Sub 

“我有一个用户定义的函数(= MyFunction()),可以从Excel表中调用。因此,我也有一个菜单button,我需要调用所有函数调用= MyFunction(),当用户点击一个button。“

这可以很容易地完成,而不需要创buildcaching来存储一系列的单元格。 但是,您需要注意计算方法。 我相信下面的代码可以确保你的范围总是被计算出来的,但是:(1)如果calc方法不是手工的,那么Excel最终将控制什么是计算的,何时以及为什么,所以它也可以重新计算其他单元格。 (2)我再次相信它保证重新计算所有单元格与你的函数无关的计算方法,但我没有testing下面的代码表和半自动计算方法。

下面的代码提供了两种方法:

(1) – 重新计算包含公式的所有单元格:优点是跳过循环和其中的代码,缺点是可能会强制重新计算比您真正需要的更多的单元格。

(2) – build立一个范围的兴趣和重新计算范围:缺点是build立这个范围可能需要一些严重的计算工作。 好处是,如果calc方法设置为manual,那么我相信Excel将只重新计算该范围内的单元格。

我猜这个select取决于你需要解决的问题的具体细节。

“我的计划是在MyFunction()内部,为调用单元指定一个名称rerefence并将其存储在vba中,这样我就可以得到一个单元格名称数组,然后当点击菜单button时可以调用这些单元格引用。

如果你真的想要遵循这个方法,或者你确实需要为了描述的目的创build一个单元格的caching,那么可以这样做,尽pipe它是基本的,但它甚至可以以保存Excel会话。 但是,这需要更多的工作,更高级的方法,而且还是相当不成熟的。 国际海事组织,这个问题明显矫枉过正。 更糟糕的是,每次单元更新时都必须调用代码,以确保caching保持最新状态,这可能会影响性能。 至于GSerg的build议:这个方法 – 正如他自己所说的 – 并不能真正控制caching本身的生命。 这是每次你到达caching,你将不得不检查,如果Excel已经抹去了,如果是这样的话,重build它。

结论:我build议你不要caching细胞。 相反,我build议你试着find需要按需求重新计算的单元格,并强制重新计算那些可以find的最佳方法。 还是不服气? 在这种情况下,使用Application.Caller.Address (请参阅下面的代码)来检索调用您的函数的单元格的地址。

备注:在Excel 2003中实施和testing。为了格式化目的,包含C#样式的注释符号。

 Option Explicit Public Sub ReEvaluateMyFunction() On Error GoTo Handle_Exception Dim targetCells As Range Dim targetCell As Range Dim rangeToRecalc As Range /*'Workbook and worksheet names 'hard-coded for the example*/ Set targetCells = Application _ .Workbooks("Book1") _ .Worksheets("Sheet1").UsedRange _ .SpecialCells(xlCellTypeFormulas) If targetCells Is Nothing Then Exit Sub /*'You can narrow down the range if you know 'more about the function's return type, eg: '.SpecialCells(xlCellTypeFormulas, xlNumbers) '.SpecialCells(xlCellTypeFormulas, xlTextValues) 'OPTION 1: re-calculate all cells in the range 'Remark: unless calc method is set to "Manual", which 'should give you full control, I think there's no 'guarantee that other cells will not be recalculated*/ If Application.Calculation = xlCalculationManual Then //'Use to force recalculation if calc mode is manual targetCells.Calculate Else //'Use this to force recalculation in other cases targetCells.Dirty End If Set targetCells = Nothing Exit Sub /*'OPTION 2: create a range specific to your 'function and recalculate that range*/ For Each targetCell In targetCells If targetCell.Formula = "=MyFunction()" Then If rangeToRecalc Is Nothing Then Set rangeToRecalc = targetCell Else Set rangeToRecalc = Union(rangeToRecalc, targetCell) End If End If Next targetCell //'Same comments as before If Application.Calculation = xlCalculationManual Then rangeToRecalc.Calculate Else rangeToRecalc.Dirty End If Set rangeToRecalc = Nothing Set targetCell = Nothing Set targetCells = Nothing Exit Sub Handle_Exception: Set rangeToRecalc = Nothing Set targetCell = Nothing Set targetCells = Nothing MsgBox "An error has been found: " + Err.Description, vbCritical End Sub Public Function MyFunction() As String MyFunction = Application.Caller.Address End Function