在列表框中显示超过10列的数据

问题描述 投票:0回答:1

我需要有关此错误的帮助我有一个包含20个文本框和一个列表框的用户窗体,代码在填充20个文本框后应在列表框中显示数据。我在互联网上搜索了很多,发现此代码对某人有效,但我不知道确切的错误在哪里我感谢有人帮我

Private Sub CommandButton1_Click()
Dim arr1 As Variant
arr1 = Array(Me.TextBox1, Me.TextBox2, Me.TextBox3, Me.TextBox4, Me.TextBox5, Me.TextBox6, 
Me.TextBox7, Me.TextBox8, Me.TextBox9, Me.TextBox10, Me.TextBox11, Me.TextBox12, Me.TextBox13, 
Me.TextBox14, Me.TextBox15, Me.TextBox16, Me.TextBox17, Me.TextBox18, Me.TextBox19, Me.TextBox20)
With ListBox1
.AddItem
For i = LBound(arr1) To UBound(arr1)
.List(.ListCount - 1, i) = arr1(i).Value
Next i
End With

结束子

[https://i.stack.imgur.com/Hl4al.jpg][1]

[https://i.stack.imgur.com/7ZBPi.jpg][2]

excel-vba
1个回答
0
投票

一个人可以使用AddItem方法添加的列数限制为10列。请尝试以下操作...

Option Explicit

Private Sub CommandButton1_Click()

    With Me.ListBox1
        If .ListCount = 0 Then
            GetFirstRow
        Else
            GetNextRow
        End If
    End With

End Sub

Private Sub GetFirstRow()

    Dim arr(1 To 1, 1 To 3) As Variant
    Dim i As Long

    For i = LBound(arr, 2) To UBound(arr, 2)
        arr(1, i) = Me.Controls("TextBox" & i).Value
    Next i

    Me.ListBox1.list = arr()

End Sub

Private Sub GetNextRow()

    Dim arr() As Variant
    Dim i As Long

    With Me.ListBox1
        arr() = Application.Transpose(.list())
        ReDim Preserve arr(1 To UBound(arr, 1), 1 To UBound(arr, 2) + 1)
        For i = LBound(arr, 1) To UBound(arr, 1)
            arr(i, UBound(arr, 2)) = Me.Controls("TextBox" & i).Value
        Next i
    End With

    Me.ListBox1.list = Application.Transpose(arr())

End Sub
© www.soinside.com 2019 - 2024. All rights reserved.