excel - 列表框错误 "Could not Set the List property. Invalid Property Value."

标签 excel vba listbox userform

我有一个带有列表框、文本框和组合框以及一个保存按钮的用户窗体。下面是我的保存按钮代码。

Private Sub cmdsave_Click()
Dim x As Integer
        x = Me.ListBox1.ListCount

If Me.cmbtrans.Value = "Debit" Then
    With Me.ListBox1
        .Enabled = True
        .ColumnCount = 13
        .ColumnWidths = "49.95 pt;10 pt;114.95 pt;10 pt;114.95 pt;10 pt;114.95 pt;10 pt;75 pt;10 pt;49.95 pt;10 pt;49.95 pt"
            .AddItem
            .List(x, 0) = Me.txtdate
            .List(x, 1) = "|"
            .List(x, 2) = Me.txtgrouphead
            .List(x, 3) = "|"
            .List(x, 4) = Me.txtcontrolhead
            .List(x, 5) = "|"
            .List(x, 6) = Me.cmbaccounthead
            .List(x, 7) = "|"
            .List(x, 8) = Me.cmbtrans
            .List(x, 9) = "|"
            .List(x, 10) = Me.txtamount
      End With

End If

End Sub

它在添加项目级别显示错误 .List(x, 10) = Me.txtamount .
但是,它运行平稳,直到添加项目级别 .List(x, 9) = "|" .

我无法理解为什么它在最后一行给出错误。

最佳答案

克服 10 列限制的数组方法
.AddItem方法的限制和默认设置仅为 10列可以在列表框(或组合框)中创建; List索引从零开始,您最多只能添加 .List(x, 9) .

如果你想克服这个内置限制,你必须使用允许将整个数组分配给 .List 的 Array 方法。属性(property)在一份声明中。

当您在每次单击事件时增加列表框行元素以添加新控件值时,您必须通过一个新行元素重新调整整个数据集的尺寸。

一个 ReDim Preserve然而,语句只能在其最后一维中执行。因此,您必须使用 2-dim 数组,其中“行”索引位于不变的“列”索引之后,以提供正确尺寸的数组。

技巧:而不是重新转置此数组并将其分配回列表框.List属性,您可以使用 .Column属性,而不是已经接受倒置(=转置)维度顺序。

示例代码

为方便起见,添加了控件名称字符串,因为它允许通过 Controls 在循环中获取使用的控件值允许按名称引用它们的集合。

Private Sub cmdSave_Click()
' Define a control names string to be used to append to Listbox1
  Const USEDCONTROLS$ = "txtdate,txtgrouphead,txtcontrolhead,cmbaccounthead,cmbtrans,txtamount"
  Dim x&, i&, v, myCtrls
  myCtrls = Split(USEDCONTROLS, ",")        ' create flat array myCtrls out of control names

  x = Me.ListBox1.ListCount                 ' get current list count

If Me.cmbtrans.value = "Debit" Then
  With Me.ListBox1
    .Enabled = True
    .ColumnWidths = "49.95 pt;10 pt;114.95 pt;10 pt;114.95 pt;10 pt;114.95 pt;10 pt;75 pt;10 pt;49.95 pt;10 pt;49.95 pt"
    .ColumnCount = 13

    If .ListCount > 0 Then
       ' [1] write existing listbox elements to array(column index, row index)
         v = .Column
       ' [2] increment to new row index x in 2nd (sic!) dimension
       '     as a ReDim Preserve can only change the last array dimension.
       '     (with the .Column property you are using the
       '     transposed counterpart of the .List property)
         ReDim Preserve v(UBound(v), x)
    Else
       ' [1-2] redimension array v the first time (no existing listbox values so long)
         ReDim v(.ColumnCount - 1, 0)           ' 13 counted columns equal a zerobased index of 12
    End If

    ' [3a] assign current values to array
      For i = 0 To UBound(myCtrls)
          v(i * 2, x) = Me.Controls(myCtrls(i))
      Next i
    ' [3b] write separators after each data input
      For i = 1 To UBound(v) - 1 Step 2: v(i, x) = "|": Next i

    ' [4] reassign array to listbox property .Column (~ transposed .List property)
    .Column = v
  End With

End If

End Sub

关于excel - 列表框错误 "Could not Set the List property. Invalid Property Value.",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54204164/

相关文章:

vba - 在 VBA 中监听使用 WinAPI 创建的控件中的 Windows 消息

c# - 在不更改所选项目的情况下聚焦 wpf 列表框

c# - ListBox.SelectedIndexChanged - 你能确定它是否是用户发起的吗?

excel - 取消时Application.InputBox错误424

javascript - 提高创建 Excel 工作表的速度(以编程方式)

excel - 是否可以创建一个不活动的新工作簿?

php - 将数据从 VBA (excel) 发送到 php,反之亦然

源更改时 VBA 刷新用户窗体列表框数据

vba - 从 Excel VBA 控件内部进行自引用

excel - 将公式粘贴到单元格时如何避免Excel VBA中的@字符