将数组值写入工作表

如果我input如下所示的UDF作为数组公式,我可以写入数组x(n,3)中的值。 (下面的UDF仅用于演示目的,实际的任务涉及由数字n确定的数组维度的频繁更改)通过突出显示范围(超过150行),而不是以数组公式formsinputUDF时间,我尝试使用“resize”function来自动执行此过程。 我得到“#Value!” 单元格“K1”中的错误。 它也给了我“#Value!” 如果我inputUDF作为数组公式。

请指教。

Function Loadnumbers(n%, alpha#) Dim Destination As Range Set Destination = Range("K1") Dim I As Integer ReDim x(n, 3) As Double For I = 1 To n For J = 1 To 3 x(I, J) = (x(I, 1) + 1) * alpha x(I, J) = (x(I, 1) + 2) * alpha x(I, J) = (x(I, 2) + 2) * alpha Next J Next I Destination.Resize(UBound(x, 1), UBound(x, 3)).Value = x() 'The above statement gives me "#Value!" error either in a single cell ("K1") or the array. 'Loadnumbers = x() 'The above statement works using this statement as I enter as an array formula End Function 

您不能用函数更改其他单元格的内容: https : //support.microsoft.com/en-us/kb/170787

如果你想改变另一个单元,那么你将不得不使用一个子。 如果您将子项与事件(例如Worksheet_Change或Worksheet_SelectionChange)耦合,那么您可以得到类似的结果。 但是为了避免太慢Excel的速度,我build议只更改表格或者表单上的一些单元格可以触发这样的事件。 例:

 Private Sub Worksheet_Change(ByVal Target As Range) If Intersect(Target, Range("A1:C4")) Is Nothing Then Exit Sub Application.EnableEvents = False 'Do you things Application.EnableEvents = True End Sub