[RESOLVED] Excel VBA - Auto Load Combo Box
This has got to be the noob question of all noob questions, but I need a combo box preloaded with entries when the excel file opens. Why doesn't this work?
Code:
Private Sub Worksheet_Activate()
cboReimbMthd.AddItem "STD"
cboReimbMthd.AddItem "LOUwBD"
cboReimbMthd.AddItem "Model"
End Sub
Re: Excel VBA - Auto Load Combo Box
Load it in Sub Workbook_Open(). Is that the combobox in Sheet1?
Code:
Private Sub Workbook_Open()
With ThisWorkbook.Worksheets(1).cboReimbMthd
.AddItem "STD"
.AddItem "LOUwBD"
.AddItem "Model"
End With
End Sub
Re: Excel VBA - Auto Load Combo Box
I looked for Open, and couldn't find it....that is why I choose Activate.
I see these possible events:
Activate
BeforeDoubleClick
BeforeRightClick
Calculate
Change
Deactive
FollowHyperLink
PivotTableUpdate
SelectionChange
Re: Excel VBA - Auto Load Combo Box
That's because you are looking at the Sheet events, rather than the Book ones. ;)
Re: Excel VBA - Auto Load Combo Box
Open ThisWorkbook code module instead of Sheet code module.
You can also define a Sub or Function to fill the combo box then call that within Workbook_Open() event.
1 Attachment(s)
Re: Excel VBA - Auto Load Combo Box
Quote:
looked for Open, and couldn't find it....that is why I choose Activate.
Further to what Anhn suggested, Does this help?
Re: Excel VBA - Auto Load Combo Box
Quote:
Originally Posted by anhn
Open ThisWorkbook code module instead of Sheet code module.
You can also define a Sub or Function to fill the combo box then call that within Workbook_Open() event.
I have the sub that does. The problem is that it isn't being called when the file opens.
So, both you and koolsid are saying to do Workbook instead of sheet.
Ok....I will try that. My question now would be do I leave my Public Sub on the Sheet or move it to the Workbook or does it matter?
Re: Excel VBA - Auto Load Combo Box
you will have to move it in the workbook
Re: Excel VBA - Auto Load Combo Box
1. If the combobox need to fill only once when opening the workbook then you can put code in ThisWorkbook code module as below:
Code:
Option Explicit
Private Sub Workbook_Open()
With Sheet1.cboReimbMthd
.Clear
.AddItem "STD"
.AddItem "LOUwBD"
.AddItem "Model"
End With
End Sub
2. If you intend to refill the combo again later then it's better to create a Sub in Sheet1:
Code:
Option Explicit
Sub FillComboBox()
With Me.cboReimbMthd
.Clear
.AddItem "STD"
.AddItem "LOUwBD"
.AddItem "Model"
End With
End Sub
And Call it in Workbook_Open(). You should qualify the sub with the sheet codename:
Code:
Option Explicit
Private Sub Workbook_Open()
Sheet1.FillComboBox
End Sub
Re: Excel VBA - Auto Load Combo Box
Thanks guys, it is working fine.
Re: Excel VBA - Auto Load Combo Box
Re: [RESOLVED] Excel VBA - Auto Load Combo Box
Quote:
but not bale to populate the combox
you have told us nothing, have not shown the code you have tried so far, not described what you want to achieve, and dragged up an old thread that was already resolved