Public Function Myfunction(Roundnumber As Integer, teams As Integer) As Range
Dim i As Integer Dim j As Integer Dim Result() As 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
End Function
Since you haven't declared Result
anywhere with a Dim
, when you write:
Result(i) = i + j
then VBA sees Result
as a Sub or Function in which you pass the value of i
. Since that Sub or Function doesn't exist in your code, then you get the error you're seeing.
You need to do something like this before your first For
loop starts:
Dim Result() As Integer
ReDim Result(1 To Teams)
which will declare Result
as being an array of integers and ReDim
it to the size of Teams
. You need to give it the right dimensions because otherwise VBA won't know how big it is and won't be able to store anything inside it.
Note that ReDim
is an expensive operation so you do it only once when you know the size of the array.
'I got it to work like this
Public Function Myfunction(Roundnumber As Range, teams As Variant) As Variant
Dim i As Long Dim j As Long Dim Result() As Variant ReDim Result(0 To teams, 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
End Function
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.