问题
At the moment I'm working with making a userform with 40 combobox's all which have the same list. My problem is filling all those combobox's is making the userform.show
slow. The list that gets populated in those combobox's is a very long list (46542 rows and list length can vary) the list is with 3 columns.
I have been fooling around with CONCATENATE the whole list but that doesn't make much of a change. Also because I need to have the value when selected in the combobox to be CONCATENATE with all 3 columns in the combobox etc. when selecting row no. 1 in the combobox instead of writing only column 1 in the comboxbox textfield it will return all 3 columns so that means I'm actually having 4 columns where the first is CONCATENATE and hidden in the dropdown.
So my question is, is there a way to do the process more light?
So here is the code:
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
This code goes on until it hit combox40
My old code was working pretty fast but it didn't have the column that was concatenated
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
回答1:
Instead of
ComboBox3.List = tsheet.Range("A2:l" &
Worksheets("Players").Cells(Rows.Count, 1).End(xlUp).Row).Value
use something like this (declare Arr as 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
Instead of repeating the same code 40 times, create a loop.
For i = 1 to 40
Cbx = Me.Controls("ComboBox" & Cstr(i))
' then manipulate Cbx as you have done.
Next I
Finally, since your 40 comboboxes are all the same, why not make do with only 1? You can move it around from row to row, let the user make his selection and transfer that selection to a textbox that appears in the place of the Cbx on Exit. When you click on the Tbx again it is substituted by the Cbx so that you have access to the list again.
回答2:
In the module:
Dim ArrPlayers() as integer
In the userform initialization:
'To Do: add code to populate listbox with players
ReDim ArrPlayers (0 To 39)
On the listbox change event:
txtPosition.text = ArrPlayers(lstPlayers.ListIndex)
On the textbox change event:
ArrPlayers(lstPlayers.ListIndex) = cInt(txtPosition.text)
You will need to then save the values.
回答3:
use the RowSource
property of the combobox control
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
来源:https://stackoverflow.com/questions/46389412/populate-multiple-comboboxs-makes-vba-userform-slow