2015-06-03 88 views
0

看來,當我第一次點擊組合框,然後單擊箭頭時,所有項目都顯示出來。有時ActiveX組合框只顯示一行,爲什麼?

雖然如果我點擊箭頭之前沒有點擊組合框,只顯示一個項目,我可以點擊滾動按鈕來查看其他項目。

爲什麼會發生這種情況?

這裏是我使用的填充物品

Private Sub ComboBox1_GotFocus() 
    Dim c As Range 
    Dim selText As String 
    selText = ComboBox1.selText 
    ComboBox1.Clear 
    For Each c In wConfig.Range("BudgetDropdown").Cells 
     ComboBox1.AddItem c.Value 
    Next c 
    ComboBox1.selText = selText 
End Sub 

enter image description here

組合框宏enter image description here

+0

請考慮使用ComboBox的'ListFillRange'屬性。如果您將其設置爲「BudgetDropdown」,則列表應自動填充。 – BrakNicku

+0

這聽起來像個好主意。但是,ComboBox1.ListFillRange = wConfig.Range(「BudgetDropdown」)會拋出一個錯誤,提示「類型不匹配」 – user1283776

+0

您應該使用ComboBox1.ListFillRange =「BudgetDropdown」',或者直接在屬性編輯器中將其設置爲'BudgetDropdown'。只要確保名稱是用工作簿範圍定義的或者它在同一個工作表中。 – BrakNicku

回答

1

自動填充與命名範圍內的數據組合框,設置它的ListFillRange屬性設置範圍的名稱。在屬性窗口,將其設置爲BudgetDropdown

ComboBox1.ListFillRange = "BudgetDropdown" 

或者:

您可以在運行時做到這一點。

+0

這似乎有一個缺點。每當工作表上的值發生更改時,都會觸發Combobox1_change事件。 http://www.mrexcel.com/forum/excel-questions/603076-combobox-change-event-triggers-value-change-worksheet.html – user1283776

+0

@ user1283776你對「BudgetDropdown」的定義是什麼?鏈接的線程? – BrakNicku

+0

這是Excel中的一個命名區域,由偏移量和計數函數組成。也許對我來說更好的解決方案是刪除組合框的所有項目,然後像讀取它們一樣讀取它們?但在我的原始代碼中,我遇到了這樣的問題:.clear似乎不僅刪除所有項目,而且還更改了我不希望它執行的組合框的值/文本。是否可以刪除所有項目,同時保留組合框中寫入的值/文本不受影響 – user1283776

0

不知道到底是什麼原因,但基本上當您打開剛剛清理過的組合框時,它只顯示1條車道,因爲即使您剛剛重新填充它,它也會「認爲」它爲空。要欺騙它,你必須逐個刪除所有行,直到你達到你的ListCount值(你的組合框將顯示的行數)。然後,您可以添加所有新行,然後可以刪除以前省略的行。在我的情況下,我不能使用ListFillRange,因爲我的列表「embended」在幾個單元格中;所以我不得不提取它。類似這樣的:

Private Sub Combobox3_GotFocus() 

Dim RngRange01 As Range 'Single cell where my row is "embended" 
Dim IntCounter01 As Integer 'A counter 
Dim BlnCheck as Boolean 'A boolean to remember me if it's the Combobox was empty before i 
'focused it 

IntCounter01 = ComboBox3.ListCount 'Set the counter equal to ListCount 

'I check if the combobox is already filled or not and modify the BlnCheck 
BlnCheck = True 
If ComboBox3.ListCount = 0 Then BlnCheck = False 

'In this For-Next i remove all the rows till i reach the ListRows 
For IntCounter01 = IntCounter01 To ComboBox3.ListRows + 1 Step -1 
    ComboBox3.RemoveItem IntCounter01 - 1 
Next IntCounter01 

'Set the range (it's a named list with titles located in another sheet) 
Set RngRange01 = Sheets("MySheet1").Cells(2, Sheets("MySheet1").Range("MyList").Column) 


Do Until RngRange01.Value = "" 'Cover the whole list 

    'This If is to select the right data to insert (originally it was more complicated 
    'so i've cut it for this explanation) 
    If RngRange01.Offset(0, 1).Value <> RngRange01.Offset(-1, 1).Value Then 
     ComboBox3.AddItem RngRange01.Offset(0, 1).Value 
    End If 

    Set RngRange01 = RngRange01.Offset(1, 0) 'Next cell of the list 
Loop 

'Now we can remove the rows of the combobox we didn't remove previously (if the combobox 
'wasn't empty already) 
If BlnCheck = True then 
    For IntCounter01 = ComboBox3.ListRows To 1 Step -1 
     ComboBox3.RemoveItem IntCounter01 - 1 
    Next IntCounter01 
End If 

End Sub 

到目前爲止,它只會在您第一次關注組合框後才能正常工作。仍然很煩人!爲了徹底刪除(欺騙)錯誤,您必須在組合框中添加一些通道,然後才能將其聚焦;也許當你打開這樣的工作簿:

Private Sub Workbook_Open() 

Dim IntCounter01 As Integer 'A counter 

IntCounter01 = 1 'Set the counter 

'In this For-Next we add a lane till we reach the ListRows 
For IntCounter01 = IntCounter01 To Sheets("MySheet2").ComboBox3.ListRows 
    Sheets("MySheet2").ComboBox3.AddItem "" 
Next IntCounter01 

End Sub 
相關問題