我在Excel中制作了一堆2D数组,并且我编写了一个函数,将相同的数据放在每个数据的副本中。我不太确定我的语法部分是否正确。
该函数称为'Fill',旧数组'Old'和新数组'New'。我使用名称'Block'作为传递变量名。
所以,我的代码中的行是:
New = Fill(Block:=Old())
我的第一行功能是:
Function Fill(Block() As Variant) As Variant
这给了我一个'Old'数组的类型不匹配错误,说它期待一个数组。导致我认为该功能正常,等待正确的类型,但没有收到它。
我错过了什么?
答案 0 :(得分:1)
自从我做VBA编程以来已经有一段时间了,但我认为以下内容更可能是正确的:
NewArray = Fill(OldArray)
Function Fill(Block As Variant) As Variant
Fill = Block
End Function
答案 1 :(得分:0)
以下是您可能遇到错误的原因。如果函数需要特定类型,则必须将该变量声明为该类型。
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