如果我输入下面显示的UDF作为数组公式,我能够在数组x(n,3)中写入值。 (下面的UDF仅用于演示目的。实际任务涉及由数字n确定的数组维度的频繁更改。)而不是通过突出显示范围(超过150行)而将UDF作为数组公式输入时间,我尝试使用“调整大小”功能自动化该过程。我得到了“#Value!”单元格“K1”中的错误。它还给了我“#Value!”如果我将UDF作为数组公式输入。
请告知。
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
答案 0 :(得分:0)
您无法使用函数更改其他单元格的内容: https://support.microsoft.com/en-us/kb/170787
如果您想要更改另一个单元格,则必须使用sub。如果您将sub与事件(例如Worksheet_Change或Worksheet_SelectionChange)相结合,那么您可以获得类似的结果。但是为了避免过多地减慢Excel的速度,我建议只有更改或工作表上的一些单元格才能触发这样的事件。例如:
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