Excel vba 列表框多列多行来自14个文本框
Excel vba list box multi column multi row from 14 text boxes
我的用户表单有 14 个文本框、2 个命令按钮“下一步”、“Post”和 1 个列表框
我需要代码将数据从 14 个文本框获取到列表框,当用户输入新数据并按下下一步时,再次将此数据添加到列表框的第二行,获得,再次
最后,当他按下 post 时,所有数据都开始工作 sheet“数据库”
Sub CommandButton1_Click()
Dim arr1, i As Long
Dim arr2(0 To 0, 0 To 13)
arr1 = Array(TB10, TB10, TB0, tb1, cb1, cb2, tb5, tb4, TB10, TB10, TB10, tb6, tb7, tb8)
For i = 0 To UBound(arr1)
arr2(0, i) = arr1(i)
Next i
ListBox1.List = arr2
End Sub
但是这段代码只是将一次数据添加到列表框中,我需要添加更多行♥
"...需要添加更多行"
通常您会将完整的 (d) 数据集分配给 ListBox1
的 .List
属性(您选择将其命名为 arr2
)。
由于您想增加每个 CommandButton1_Click()
事件包含的元素行数并保留所有现有数据,理论上您需要增加二维数组的第一维 - 但这是不可能的使用 ReDim Preserve
。
要解决这个问题,只需反转 arr2
的维度,从而在其第一个维度中定义 14 列值,将 "row" 维度定义为第二个维度。列表框控件提供了一个 .Column
属性 ,您可以使用它代替通常的 .List
属性 来写回整个数据集(无需关心故意转置的行& 列)。
备注
当您更改 OP 中的代码时,我假设 tb0
、tb1
、...对应于枚举的 TextBox 控件。 (请根据您的需要更改控件数组 arr1
中有些奇怪的顺序。)
示例代码
Option Explicit ' declaration head of userform code module
Dim arr2() ' make arr2 values disponible for each CommandButton1_Click event
Sub CommandButton1_Click()
' declare/assign variables
Dim arr1(), i As Long, nxt As Long
arr1 = Array(tb0, tb1, tb2, tb3, tb4, tb5, tb6, tb7, tb8, tb9, tb10, tb11, tb12, tb13) ' <~~ Change order to your needs
' define index of next row in listbox
nxt = Me.ListBox1.ListCount ' ListCount automatically counts upper bound + 1
' a) you can only increment an array's last dimension, so ...
' b) redefine arr2 with new elements in its 2nd dimension
ReDim Preserve arr2(0 To UBound(arr1), 0 To nxt)
' assign textbox and combobox values to arr2
For i = 0 To UBound(arr1)
arr2(i, nxt) = arr1(i)
Next i
' reassign arr2 to the listboxes .Column property (instead of the .List property)
ListBox1.Column = arr2
End Sub
Private Sub UserForm_Layout()
With Me.ListBox1
.ColumnCount = 14 ' define column count
.ColumnWidths = "50;50;50;50;50;50;50;50;50;50;50;50;50;50" ' <~~ change to your needs
' .Width = 50 * .ColumnCount + 16
End With
End Sub
请允许我发表评论:我认为这回答了您最初的问题。您会找到足够多的示例,了解如何将数据移回阅读 Whosebug 站点的工作表,但这需要用显示您目前已尝试过的代码来制定一个新问题 - 请参阅 How to create a Minimal, Complete, and Verifiable example。
我的用户表单有 14 个文本框、2 个命令按钮“下一步”、“Post”和 1 个列表框
我需要代码将数据从 14 个文本框获取到列表框,当用户输入新数据并按下下一步时,再次将此数据添加到列表框的第二行,获得,再次
最后,当他按下 post 时,所有数据都开始工作 sheet“数据库”
Sub CommandButton1_Click()
Dim arr1, i As Long
Dim arr2(0 To 0, 0 To 13)
arr1 = Array(TB10, TB10, TB0, tb1, cb1, cb2, tb5, tb4, TB10, TB10, TB10, tb6, tb7, tb8)
For i = 0 To UBound(arr1)
arr2(0, i) = arr1(i)
Next i
ListBox1.List = arr2
End Sub
但是这段代码只是将一次数据添加到列表框中,我需要添加更多行♥
"...需要添加更多行"
通常您会将完整的 (d) 数据集分配给 ListBox1
的 .List
属性(您选择将其命名为 arr2
)。
由于您想增加每个 CommandButton1_Click()
事件包含的元素行数并保留所有现有数据,理论上您需要增加二维数组的第一维 - 但这是不可能的使用 ReDim Preserve
。
要解决这个问题,只需反转 arr2
的维度,从而在其第一个维度中定义 14 列值,将 "row" 维度定义为第二个维度。列表框控件提供了一个 .Column
属性 ,您可以使用它代替通常的 .List
属性 来写回整个数据集(无需关心故意转置的行& 列)。
备注
当您更改 OP 中的代码时,我假设 tb0
、tb1
、...对应于枚举的 TextBox 控件。 (请根据您的需要更改控件数组 arr1
中有些奇怪的顺序。)
示例代码
Option Explicit ' declaration head of userform code module
Dim arr2() ' make arr2 values disponible for each CommandButton1_Click event
Sub CommandButton1_Click()
' declare/assign variables
Dim arr1(), i As Long, nxt As Long
arr1 = Array(tb0, tb1, tb2, tb3, tb4, tb5, tb6, tb7, tb8, tb9, tb10, tb11, tb12, tb13) ' <~~ Change order to your needs
' define index of next row in listbox
nxt = Me.ListBox1.ListCount ' ListCount automatically counts upper bound + 1
' a) you can only increment an array's last dimension, so ...
' b) redefine arr2 with new elements in its 2nd dimension
ReDim Preserve arr2(0 To UBound(arr1), 0 To nxt)
' assign textbox and combobox values to arr2
For i = 0 To UBound(arr1)
arr2(i, nxt) = arr1(i)
Next i
' reassign arr2 to the listboxes .Column property (instead of the .List property)
ListBox1.Column = arr2
End Sub
Private Sub UserForm_Layout()
With Me.ListBox1
.ColumnCount = 14 ' define column count
.ColumnWidths = "50;50;50;50;50;50;50;50;50;50;50;50;50;50" ' <~~ change to your needs
' .Width = 50 * .ColumnCount + 16
End With
End Sub
请允许我发表评论:我认为这回答了您最初的问题。您会找到足够多的示例,了解如何将数据移回阅读 Whosebug 站点的工作表,但这需要用显示您目前已尝试过的代码来制定一个新问题 - 请参阅 How to create a Minimal, Complete, and Verifiable example。