ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Discussion (Misc queries) (https://www.excelbanter.com/excel-discussion-misc-queries/)
-   -   Clear entries on Combo box (https://www.excelbanter.com/excel-discussion-misc-queries/122441-clear-entries-combo-box.html)

casey

Clear entries on Combo box
 
I found code below while searching the net. It works, but how do you clear
the entry from the list when workbook is opened. For some reason, the
cboMonth says "August" instead of blank?

Private Sub Workbook_Open()
With Sheet1.cboName
.ListFillRange = ""
.AddItem "John"
.AddItem "Jay"
.AddItem "Mark"
.AddItem "Matt"
.ListIndex = 0
End With
End Sub


Private Sub cboName_Change()
For IndexNbr = cboMonth.ListCount - 1 To 0 Step -1
cboMonth.RemoveItem (IndexNbr)
Next IndexNbr

Select Case cboName.Value

Case "John"
cboMonth.AddItem "Jan"
cboMonth.AddItem "February"
cboMonth.AddItem "March"

Case "Jay"
cboMonth.AddItem "April"
cboMonth.AddItem "May"
cboMonth.AddItem "June"

Case "Mark"
cboMonth.AddItem "July"
cboMonth.AddItem "August"
cboMonth.AddItem "September"

Case "Matt"
cboMonth.AddItem "October"
cboMonth.AddItem "November"
cboMonth.AddItem "December"
Case Else
MsgBox cboName.Value & " Is Not Valid."

End Select
End Sub



Dave Peterson

Clear entries on Combo box
 
If August is the value in that cboMonth box, then it's never being reset. It's
just the last value you had in that combobox when the workbook was saved.

Try clearing the cboMonth in the workbook_open event:

Option Explicit
Private Sub Workbook_Open()

Sheet1.cboMonth.Clear
Sheet1.cboMonth.Value = ""

With Sheet1.cboName
.ListFillRange = ""
.AddItem "John"
.AddItem "Jay"
.AddItem "Mark"
.AddItem "Matt"
.ListIndex = 0
End With

End Sub

casey wrote:

I found code below while searching the net. It works, but how do you clear
the entry from the list when workbook is opened. For some reason, the
cboMonth says "August" instead of blank?

Private Sub Workbook_Open()
With Sheet1.cboName
.ListFillRange = ""
.AddItem "John"
.AddItem "Jay"
.AddItem "Mark"
.AddItem "Matt"
.ListIndex = 0
End With
End Sub

Private Sub cboName_Change()
For IndexNbr = cboMonth.ListCount - 1 To 0 Step -1
cboMonth.RemoveItem (IndexNbr)
Next IndexNbr

Select Case cboName.Value

Case "John"
cboMonth.AddItem "Jan"
cboMonth.AddItem "February"
cboMonth.AddItem "March"

Case "Jay"
cboMonth.AddItem "April"
cboMonth.AddItem "May"
cboMonth.AddItem "June"

Case "Mark"
cboMonth.AddItem "July"
cboMonth.AddItem "August"
cboMonth.AddItem "September"

Case "Matt"
cboMonth.AddItem "October"
cboMonth.AddItem "November"
cboMonth.AddItem "December"
Case Else
MsgBox cboName.Value & " Is Not Valid."

End Select
End Sub


--

Dave Peterson


All times are GMT +1. The time now is 08:39 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com