0

一个 sub 调用一个函数,该函数依次填充并返回一个数组。问题是我需要知道我的子中该数组的大小。是否有一条快速线可以找到该数组的大小?我知道我可以用一个小循环来做到这一点,这似乎是不必要的。事情是这样的:

Sub innout(lastupdate As Long, lrcr As Long)

Dim numclosed As Long, numnew As Long
Dim lblvar As Variant
Dim tblcr As Range
Dim finishdates As Range
Dim msg As Long
Dim resp As String, ops() As Long, cps() As Long
Dim i As Long

    listallsupv

'do stuff...
end sub



Function listallsupv() As String()

Dim las() As String

Dim lrs As Long
Dim i As Long, j As Long, r As Long
Dim supervsheet As Worksheet

Set supervsheet = Sheets("Superviseurs")

lrs = supervsheet.Range("A1").Offset(supervsheet.Rows.count - 1, 0).End(xlUp).Row

ReDim las(1 To lrs) As String

i = 1

For r = 2 To lrs
    For j = 1 To i
        If supervsheet.Range("B" & r).Value = las(j) Then
            GoTo nextj
    End If
    Next
    las(i) = supervsheet.Range("B" & r).Value
    i = i + 1
nextj:
Next

ReDim Preserve las(1 To i)

End Function

谢谢!

4

1 回答 1

0

啊,我找到了。我刚刚在我原来的 sub 中创建了数组,并调用了另一个 sub 来通过引用填充它,adnn 也返回了长度。是的,谢谢蒂姆,一开始我什至没有注意到这一点。

Sub innout(lastupdate As Long, lrcr As Long)

Dim i As Long
Dim las() As String

Call populate_las(las(), i)

'bla bla

End Sub


Sub populate_las(ByRef las() As String, ByRef i As Long)

Dim lrs As Long
Dim j As Long, r As Long
Dim supervsheet As Worksheet

Set supervsheet = Sheets("Superviseurs")

lrs = supervsheet.Range("A1").Offset(supervsheet.Rows.count - 1, 0).End(xlUp).Row

ReDim las(1 To lrs) As String

i = 1

For r = 2 To lrs
    For j = 1 To i
        If supervsheet.Range("B" & r).Value = las(j) Then
            GoTo nextj
    End If
    Next
    las(i) = supervsheet.Range("B" & r).Value
    i = i + 1
nextj:
Next

ReDim Preserve las(1 To (i - 1))

End Sub

再次感谢!

于 2013-09-12T23:36:29.120 回答