有一个excel VBA表单有多个下拉列表(超过10个)。 使用与此类似的代码填充这些下拉列表:
With Me.Controls("MyComboBox")
.Clear
Do
.AddItem MyRecordSet![MyColumn]
MyRecordSet.MoveNext
Loop Until MyRecordSet.EOF
End With
有没有办法动态设置“MyComboBox”,“MyRecordSet”,“MyColumn”对象的值?
目标是让所有组合框都在一个循环中填充,而不是一遍又一遍地拥有10多套相同的代码。
“MyComboBox”和“MyRecordSet”对象具有相似的名称,但不一样。
答案 0 :(得分:1)
Dim c As Control
dim s() as string
For Each c In Me.Controls
If TypeName(c) = "ComboBox" Then
' HERE YOU COULD GET THE RECORDSET INFO FROM THE CONTROLS TAG??
' Or using Ifs or have the name as name_prop1_prop2.....
If c.Name = "Dr1" Then
Else
End If
End If
' or
' s =split(c.name,"_")
' recordset = s(0)
' column=s(1)
next c