[英]Excel VBA: “compile error: sub or function not defined” and the VBA selects “results =”
公共功能Myfunction(整数作为整数,团队作为整数)作为范围
Dim i作为Integer Dim j作为Integer Dim Result()作为Integer ReDim Result(0 To(teams-1))
For i = 0 To (teams - 1)
For j = 0 To (teams - 1)
If Not IsError(Worksheets("Sheet2").Cells(j + 57, i + 3).Value) Then
If Roundnumber = Worksheets("Sheet2").Cells(j + 57, i + 3).Value Then
Result(i) = "yeah"
End If
End If
Next j
Next i
Myfunction = Result
结束功能
由于您在编写Dim
任何地方都没有声明Result
,因此在编写时:
Result(i) = i + j
然后VBA会将Result
视为Sub或Function,您在其中传递i
的值。 由于您的代码中不存在该Sub或Function,因此您将看到错误。
在第一个For
循环开始之前,您需要执行以下操作:
Dim Result() As Integer
ReDim Result(1 To Teams)
这将宣布Result
作为整数数组并ReDim
它的大小Teams
。 您需要给它正确的尺寸,因为否则VBA不会知道它有多大,并且无法在其中存储任何内容。
请注意, ReDim
是一项昂贵的操作,因此,只要知道阵列的大小,就只需执行一次。
``我让它像这样工作
公共功能Myfunction(整数作为范围,团队作为变量)作为变量
以Dim i作为Long Dim j作为Long Dim Result()作为Variant ReDim结果(0对团队,0)
For i = 1 To teams
For j = 1 To teams
If Not IsError(Worksheets("Sheet2").Cells(j + 56, i + 2).Value) Then
If Roundnumber = Worksheets("Sheet2").Cells(j + 56, i + 2).Value Then
Result(j - 1, 0) = j & "vs" & i
End If
End If
Next j
Next i
Myfunction = Result
结束功能
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.