2014-01-19 163 views
1

我想知道如何使用幾個下拉選項將項目添加到組合框。我附上了我的代碼,但沒有出現。有任何建議嗎?將選擇添加到Combobox

Sub ComboBox1_Change() 
Dim i As Integer 
i = ComboBox1.Items.Add("This is a new item") 
ComboBox1.SelectedIndex = i 
End Sub 
+0

究竟是你想做些什麼? –

+0

@SiddharthRout我想爲我的組合框有一個下拉列表。 – user1204868

+0

將項目添加到組合框時,它會自動添加到下拉列表 –

回答

2

您需要使用.AddItem而不是.Items.Add,也沒有分數相加項組合的變化情況。我正在使用命令按鈕添加到它。

這是你正在嘗試?

Private Sub CommandButton1_Click() 
    ComboBox1.AddItem "This is a new item" 

    '~~> After adding the item this will display the 
    '~~> last item added in the combobox 
    ComboBox1.Value = ComboBox1.List(ComboBox1.ListCount - 1) 

    '~~> Display the dropdown via code if you need this as well 
    ComboBox1.DropDown 
End Sub 

如果你想直接從組合框添加項目,那麼你也可以使用它。你輸入的東西后,這將增加項目在組合框,然後按ENTER 關鍵

Private Sub ComboBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, _ 
           ByVal Shift As Integer) 
    '~~> On Key Enter 
    If KeyCode = 13 Then 
     ComboBox1.AddItem "This is a new item" 
    End If 
End Sub 
+0

是的。我想要一個下拉選擇。有什麼設置我必須爲Excel設置?我似乎還沒有。 – user1204868

+0

組合框在哪裏?在用戶表單中?或在工作表中? –

+0

yup ..它在用戶表單上 – user1204868