填充多个combobox使VBA用户窗体变慢

目前我正在制作一个带有40个combobox的用户表单,它们都有相同的列表。 我的问题是填充所有这些combobox正在使userform.show缓慢。 在这些combobox中填充的列表是一个很长的列表(46542行和列表长度可以变化)列表是3列。

我一直在与CONCATENATE搞混,但这并没有太大的改变。 另外,因为在select行号时,在combobox中select的值与combobox中的所有3列进行CONCATENATE时相同。 1,而不是写在comboxbox文本框中只有列1,它将返回所有3列,这意味着我实际上有4列,其中第一个是CONCATENATE并隐藏在下拉列表中。

所以我的问题是,有没有办法让这个过程更轻?

所以这里是代码:

 Private Sub UserForm_Initialize() Set tsheet = ThisWorkbook.Sheets("Players") Dim v As Variant, i As Long v = tsheet.Range("A2:l" & Worksheets("Players").Cells(Rows.Count, 1).End(xlUp).Row).Value With Me.ComboBox1 .RowSource = "" .ColumnCount = 4 .BoundColumn = 2 .ColumnWidths = "1;50;50;50" 'Hide first column in dropdown For i = LBound(v) To UBound(v) .AddItem v(i, 1) & " " & v(i, 2) & " " & v(i, 3) .List(.ListCount - 1, 1) = v(i, 1) .List(.ListCount - 1, 2) = v(i, 2) .List(.ListCount - 1, 3) = v(i, 3) Next i End With With Me.ComboBox2 .RowSource = "" .ColumnCount = 4 .BoundColumn = 2 .ColumnWidths = "1;50;50;50" 'Hide first column in dropdown For i = LBound(v) To UBound(v) .AddItem v(i, 1) & " " & v(i, 2) & " " & v(i, 3) .List(.ListCount - 1, 1) = v(i, 1) .List(.ListCount - 1, 2) = v(i, 2) .List(.ListCount - 1, 3) = v(i, 3) Next i End With 

这个代码继续下去,直到它碰到combox40

我的旧代码工作得很快,但没有连接的列

 ComboBox3.ColumnWidths = "50;50;50" 'COLUMN WITH OF LISTBOX ComboBox3.ColumnCount = 3 'COLUMN NUMBER OF LISTBOX ComboBox3.List = tsheet.Range("A2:l" & Worksheets("Players").Cells(Rows.Count, 1).End(xlUp).Row).Value 

代替

 ComboBox3.List = tsheet.Range("A2:l" & Worksheets("Players").Cells(Rows.Count, 1).End(xlUp).Row).Value 

使用这样的东西(声明Arr为Variant): –

 Arr = tsheet.Range("A2:l" & Worksheets("Players").Cells(Rows.Count, 1).End(xlUp).Row).Value ' add your extra rows to the array here, followed by ComboBox3.List = Arr 

而不是重复相同的代码40次,创build一个循环。

 For i = 1 to 40 Cbx = Me.Controls("ComboBox" & Cstr(i)) ' then manipulate Cbx as you have done. Next I 

最后,由于你的40个combobox都是一样的,为什么不能只用1个呢? 您可以将它从一行移动到另一行,让用户进行select,然后将该select转移到在退出时出现在Cbx位置的文本框。 当再次单击Tbx时,它将被Cbx替代,以便您可以再次访问列表。

在模块中:

 Dim ArrPlayers() as integer 

在用户表单初始化中:

 'To Do: add code to populate listbox with players ReDim ArrPlayers (0 To 39) 

在列表框中更改事件:

 txtPosition.text = ArrPlayers(lstPlayers.ListIndex) 

在文本框更改事件:

 ArrPlayers(lstPlayers.ListIndex) = cInt(txtPosition.text) 

您将需要然后保存值。

使用combobox控件的RowSource属性

 Option Explicit Private Sub UserForm_Initialize() Dim tsheet As Worksheet Set tsheet = ThisWorkbook.Sheets("Players") Dim rs As String rs = "Players!a2:d" & tsheet.Cells(tsheet.Rows.Count, 1).End(xlUp).Row Dim aaa As Control For Each aaa In Me.Controls If Left(aaa.Name, 8) = "ComboBox" Then aaa.RowSource = rs ' =mySheet!a2:d24 in properties aaa.ControlSource = "Players!z1" ' put the chosen value into this cell (example) aaa.ColumnCount = 4 aaa.BoundColumn = 2 aaa.ColumnWidths = "1;50;50;50" ' Hide first column in dropdown End If Next aaa End Sub