主要功能是从list控件中取出所选中的内容,存入数组中供其他过程使用
Public Function arry(lst As ListBox) As String
    Dim i As Integer
    Dim j As Integer
    Dim m As Integer
    Dim n As Integer
        j = 0
        m = lst.SelCount
        n = lst.ListCount
    ReDim arry(m - 1)       此处是否可以重新定义,调试中出错,怀疑是这个问题
    For i = 0 To n - 1
        If lst.Selected(i) Then
                arry(j) = lst.List(i)
                j = j + 1
        End If
    Next i
        
End Function

解决方案 »

  1.   

    可以这样!
    Private Sub Command2_Click()
    Dim arry() As String
    Call MMMM(List1, arry())
    End SubPublic Function MMMM(ByRef lst As ListBox, ByRef arry() As String)
        Dim j As Integer
        Dim m As Integer
        Dim n As Integer
            j = 0
            m = lst.SelCount
            n = lst.ListCount
        ReDim arry(m - 1)      '
        For i = 0 To n - 1
            If lst.Selected(i) Then
                     arry(j) = lst.List(i)
                    j = j + 1
            End If
        Next i
    End Function
      

  2.   

    这样左貌似可以
    有两个疑问
       第一,这样的话那fuction可以改成sub了吧?
       第二,数组数据是否可以返回值?隐约记得数组是用同一地址空间,可以返回数值,求证一下?
      

  3.   

    第一个可以改成sub啦
    第二个没太明白,数组是要通过传址(byref)传递的 
      

  4.   

    那第一个ByRef lst As ListBox可以不用传址(byref)传递吧?请教下
      

  5.   

    Public Function arry(lst As ListBox) As Variant
        Dim i As Integer
        Dim j As Integer
        Dim m As Integer
        Dim n As Integer
        Dim arr()
            j = 0
            m = lst.SelCount
            n = lst.ListCount
        ReDim arr(m - 1)  '    此处是否可以重新定义,调试中出错,怀疑是这个问题
        For i = 0 To n - 1
            If lst.Selected(i) Then
             arr(j) = lst.List(i)
                    j = j + 1
            End If
        Next i
        arry = arr
        End FunctionPrivate Sub Form_Load()
    For i = 0 To 10
    List1.AddItem i
    Next
    End SubPrivate Sub List1_Click()
    Dim a
    a = arry(List1)
    For i = 0 To UBound(a)
    MsgBox a(i)
    Next
    End Sub