添加ComboBox项目

时间:2017-02-07 21:11:22

标签: excel vba excel-vba

有没有办法缩短这个VBA:

Me.cboDeptU1.AddItem "1"
Me.cboDeptU1.AddItem "2"
Me.cboDeptU1.AddItem "3"
Me.cboDeptU1.AddItem "4" 
Me.cboDeptU1.AddItem "5"

Me.cboDeptU.SetFocus 

我也在同一个Sub中有这个:

Me.cboDeptL1.AddItem "1"
Me.cboDeptL1.AddItem "2"
Me.cboDeptL1.AddItem "3"
Me.cboDeptL1.AddItem "4"
Me.cboDeptL1.AddItem "5"

Me.cboDeptL.SetFocus 

3 个答案:

答案 0 :(得分:5)

替换

Me.cboDeptU1.AddItem "1"
Me.cboDeptU1.AddItem "2"
Me.cboDeptU1.AddItem "3"
Me.cboDeptU1.AddItem "4" 
Me.cboDeptU1.AddItem "5"

使用

For i = 1 to 5
  Me.cboDeptU1.AddItem Cstr(i)
Next i

答案 1 :(得分:2)

如果您只是将这些项目添加到完全空List,则可以使用以下内容:

Me.cboDeptU1.List = Array("1", "2", "3", "4", "5")

答案 2 :(得分:0)