May 15th, 2008, 08:36 AM
#1
[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
May 15th, 2008, 08:43 AM
#2
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
Last edited by anhn; May 15th, 2008 at 05:18 PM .
May 15th, 2008, 09:26 AM
#3
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
May 15th, 2008, 12:00 PM
#4
Re: Excel VBA - Auto Load Combo Box
That's because you are looking at the Sheet events, rather than the Book ones.
May 15th, 2008, 05:19 PM
#5
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.
May 16th, 2008, 04:01 AM
#6
Re: Excel VBA - Auto Load Combo Box
looked for Open, and couldn't find it....that is why I choose Activate.
Further to what Anhn suggested, Does this help?
Attached Images
A good exercise for the Heart is to bend down and help another up...
Please Mark your Thread " Resolved", if the query is solved
MyGear :
★ CPU ★ Ryzen 5 5800X
★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
★ Keyboard ★ TVS Electronics Gold Keyboard
★ Mouse ★ Logitech G502 Hero
May 16th, 2008, 06:26 AM
#7
Re: Excel VBA - Auto Load Combo Box
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?
May 16th, 2008, 06:43 AM
#8
Re: Excel VBA - Auto Load Combo Box
you will have to move it in the workbook
A good exercise for the Heart is to bend down and help another up...
Please Mark your Thread " Resolved", if the query is solved
MyGear :
★ CPU ★ Ryzen 5 5800X
★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
★ Keyboard ★ TVS Electronics Gold Keyboard
★ Mouse ★ Logitech G502 Hero
May 16th, 2008, 06:54 AM
#9
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
May 16th, 2008, 11:11 AM
#10
Re: Excel VBA - Auto Load Combo Box
Thanks guys, it is working fine.
Jul 12th, 2013, 03:31 AM
#11
Lively Member
Re: Excel VBA - Auto Load Combo Box
Jul 12th, 2013, 05:54 AM
#12
Re: [RESOLVED] Excel VBA - Auto Load Combo Box
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
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid
On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
Posting Permissions
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
Forum Rules
Click Here to Expand Forum to Full Width