【问题标题】:How fill one row in listbox with array? (more than 10 columns)如何用数组填充列表框中的一行? (超过10列)
【发布时间】:2021-08-30 14:44:34
【问题描述】:

我在UserForm1 中有一个ListBox1。当我将多行数组发送到.List 时,一切正常。但是当我只发送一个单行数组时,ListBox1 中的值在第一列中排列在另一个下方。独立使用Application.Transpose。

我尝试编写一个条件和一个 for 循环,但它不起作用。 运行时错误 381 无法设置 List 属性。无效的属性数组索引。 .AddItem 超过 10 列,无法使用

您还有其他解决方案吗?

Dim sumItem As Integer: sumItem = 0 'later between 1 and 5000
.
.
ReDim Preserve arrSort(0 To (columnCount - 1 + 2), 0 To sumItem - 1)
.
.
Call Load(UserForm1) 'to be able to manipulate components
If sumItem = 1 Then 'if only one ROW is loaded in the array
Dim qq As Byte
For qq = 0 To (columnCount - 1)
UserForm1.ListBox1.List(0, qq) = arrSort(qq, 0) 'need to fill the LISTBOX ROW here
Next qq
ElseIf sumItem > 1 Then
UserForm1.ListBox1.List = Application.Transpose(arrSort) 'if more than one ROW is filled, this works
Else
End If
UserForm1.Show

感谢@Tim Williams 这对我有用:

Dim sumItem As Integer: sumItem = 0 'later between 1 and 5000
.
.
ReDim Preserve arrSort(0 To (columnCount - 1 + 2), 0 To sumItem - 1) 'the dimensions are reversed
.
.
Call Load(UserForm1) 'to be able to manipulate components
If sumItem = 1 Then 'if only one ROW is loaded in the array
Dim qq As Byte
Dim arrTmp(0 To 0, 0 To (columnCount - 1)) As Variant 'auxiliary array for dimension exchange
For qq = 0 To (columnCount - 1)
arrTmp(0, qq) = arrSort(qq, 0)
Next qq
UserForm1.ListBox1.List = arrSort
ElseIf sumItem > 1 Then
UserForm1.ListBox1.List = Application.Transpose(arrSort) 'if more than one ROW is filled, this works
Else
End If
UserForm1.Show

【问题讨论】:

    标签: arrays excel vba listbox


    【解决方案1】:

    你可以这样做:

    Const NUM_COLS As Long = 20
    
    Private Sub UserForm_Activate()
        Dim lstInit(0 To 0, 0 To NUM_COLS - 1), r As Long, c As Long
        
        Me.ListBox1.ColumnCount = NUM_COLS
        'fill a row of dummy data....
        For c = 0 To NUM_COLS - 1
            lstInit(0, c) = "R1:C" & (c + 1)
        Next c
        Me.ListBox1.List = lstInit
    End Sub
    
    Private Sub CommandButton1_Click()
        Dim arr, c As Long, ub As Long
        arr = AddARow(Me.ListBox1.List) 'get the existing listbox data and add a row
        ub = UBound(arr, 1)
        For c = 0 To UBound(arr, 2)
            arr(ub, c) = "R" & (ub + 1) & ":C" & (c + 1) 'populate the added row
        Next c
        Me.ListBox1.List = arr 'refresh the listbox
    End Sub
    
    'add one "row" to a 2D array and return the new array
    Function AddARow(lst)
        Dim lstNew, r As Long, c As Long
        ReDim lstNew(0 To UBound(lst, 1) + 1, 0 To UBound(lst, 2))
        'copy existing data
        For r = 0 To UBound(lst, 1)
            For c = 0 To UBound(lst, 2)
                lstNew(r, c) = lst(r, c)
            Next c
        Next r
        AddARow = lstNew
    End Function
    
    

    【讨论】:

    • 对不起,我的描述可能不太清楚。我不需要向现有数据添加行。我需要在空的ListBox1 中添加一个“行”。问题是具有“一行”的数组在Listbox1 中显示为一列。
    • List 属性设置为(例如)尺寸为(0 到 0、0 到 9)的数组没有问题
    • 更新了我的答案,只在UserForm_Activate中添加一行
    • 我的数组有互换的维度(0 到 9、0 到 0)。所以最简单的方法是在 for 循环中创建一个新数组,我将在哪里交换维度?
    • @T.M. - 我不是列表框的大用户,所以这只是我尝试的第一件事。很高兴知道还有一种不同的方法。
    猜你喜欢
    • 2017-08-30
    • 2021-06-07
    • 2019-04-14
    • 1970-01-01
    • 2020-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多