查找1D阵列的大小

时间:2013-09-12 23:07:10

标签: arrays excel vba excel-vba excel-2010

一个子调用一个函数反过来doest stuff并返回一个数组。探测器是我需要知道我的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

谢谢!

1 个答案:

答案 0 :(得分: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

再次感谢!