不知道到底是什麼原因,但基本上當您打開剛剛清理過的組合框時,它只顯示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
請考慮使用ComboBox的'ListFillRange'屬性。如果您將其設置爲「BudgetDropdown」,則列表應自動填充。 – BrakNicku
這聽起來像個好主意。但是,ComboBox1.ListFillRange = wConfig.Range(「BudgetDropdown」)會拋出一個錯誤,提示「類型不匹配」 – user1283776
您應該使用ComboBox1.ListFillRange =「BudgetDropdown」',或者直接在屬性編輯器中將其設置爲'BudgetDropdown'。只要確保名稱是用工作簿範圍定義的或者它在同一個工作表中。 – BrakNicku