从函数传递和接收数组

我已经在Excel中创build了一堆二维数组,并且我写了一个函数把相同的数据放在每个数据的副本中。 我不太确定我正在弄清楚语法正确的部分。

该函数被称为“填充”,旧数组“旧”和新的“新”。 我使用名称“Block”作为传递variables名称。

所以,我的代码中的行是:

New = Fill(Block:=Old()) 

而我的function的第一行是:

 Function Fill(Block() As Variant) As Variant 

这给了我一个“旧”数组的types不匹配错误,说它期待一个数组。 导致我认为function正常,并等待正确的types,但没有收到它。

我错过了什么?

我做了VBA编程已经有一段时间了,但是我认为以下几点更可能是正确的:

 NewArray = Fill(OldArray) Function Fill(Block As Variant) As Variant Fill = Block End Function 

这里有一些关于你为什么会得到错误的说明。 如果某个函数需要特定types,则必须将该variables声明为该types。

 Sub FillThis() 'Declare OldArray as Variant ' 'because that is what the function ' 'requires. ' Dim OldArray As Variant 'Similarly ...' Dim StringPart As String 'ByRef, so the variable will be ' 'changed by the function. ' 'Note that ByRef is the default. ' Fill OldArray For i = 0 To 4 Debug.Print OldArray(i) Next StringPart = "Part 1" GetString StringPart Debug.Print StringPart End Sub 'In this example, Fill is not being returned, ' 'so there is no need to declare it as anything ' Function Fill(ByRef OldArray As Variant) 'The Array must be dimensioned ' ReDim OldArray(4) For i = 0 To 4 OldArray(i) = i + 1 Next End Function Function GetString(ByRef StringPart As String) StringPart = StringPart & " Add a Bit" End Function