Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 34
Default Referencing a ComboBox in VBA

Hi,

I am using Excel 2007. I want to run some code in my Workbook_Open() Sub
that fills a combo box on one of my Worksheets (I will call it myCombo on
mySheet). I tried the following code and it did not work:

myCombo.Clear
myCombo.AddItem "Item One"
myCombo.AddItem "Item Two"
myCombo.AddItem "Item Three"

I assume that the problem is that within the Workbook_Open() Sub, the
reference to myCombo is not valid. Can anyone tell me if/how I can get a
valid reference to myCombo (on mySheet)?

Thanks in advance,
Paul Kraemer



--
Paul Kraemer
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Referencing a ComboBox in VBA

One way:

Option Explicit
Private Sub Workbook_Open()
Dim wks As Worksheet

Set wks = Me.Worksheets("mySheet")

With wks.OLEObjects("myCombo").Object
.Clear
.AddItem "item one"
.AddItem "item 2"
.AddItem "3"
End With
End Sub



Paul Kraemer wrote:

Hi,

I am using Excel 2007. I want to run some code in my Workbook_Open() Sub
that fills a combo box on one of my Worksheets (I will call it myCombo on
mySheet). I tried the following code and it did not work:

myCombo.Clear
myCombo.AddItem "Item One"
myCombo.AddItem "Item Two"
myCombo.AddItem "Item Three"

I assume that the problem is that within the Workbook_Open() Sub, the
reference to myCombo is not valid. Can anyone tell me if/how I can get a
valid reference to myCombo (on mySheet)?

Thanks in advance,
Paul Kraemer

--
Paul Kraemer


--

Dave Peterson
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,565
Default Referencing a ComboBox in VBA

You need to reference the worksheet that the combobox is on:

Worksheets(1).myCombo.Clear
worksheets(1).myCoimbo.AddItem "Item One"
etc.



"Paul Kraemer" wrote in message
...
Hi,

I am using Excel 2007. I want to run some code in my Workbook_Open() Sub
that fills a combo box on one of my Worksheets (I will call it myCombo on
mySheet). I tried the following code and it did not work:

myCombo.Clear
myCombo.AddItem "Item One"
myCombo.AddItem "Item Two"
myCombo.AddItem "Item Three"

I assume that the problem is that within the Workbook_Open() Sub, the
reference to myCombo is not valid. Can anyone tell me if/how I can get a
valid reference to myCombo (on mySheet)?

Thanks in advance,
Paul Kraemer



--
Paul Kraemer



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 489
Default Referencing a ComboBox in VBA

Give this a try. This should work if it is an ActiveX dropdown box. Hope
this helps! If so, let me know, click "YES" below.

Private Sub Workbook_Open()

With Sheets("Sheet1").myCombo
.Clear
.AddItem "Item One"
.AddItem "Item Two"
.AddItem "Item Three"
End With

End Sub
--
Cheers,
Ryan


"Paul Kraemer" wrote:

Hi,

I am using Excel 2007. I want to run some code in my Workbook_Open() Sub
that fills a combo box on one of my Worksheets (I will call it myCombo on
mySheet). I tried the following code and it did not work:

myCombo.Clear
myCombo.AddItem "Item One"
myCombo.AddItem "Item Two"
myCombo.AddItem "Item Three"

I assume that the problem is that within the Workbook_Open() Sub, the
reference to myCombo is not valid. Can anyone tell me if/how I can get a
valid reference to myCombo (on mySheet)?

Thanks in advance,
Paul Kraemer



--
Paul Kraemer

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
referencing combobox tracktraining Excel Programming 2 February 20th 09 01:32 AM
Combobox value to be set as Target for referencing from. How ? Corey Excel Programming 1 January 2nd 07 02:25 AM
Referencing Combobox with other cells in a different worksheet?? Adam Excel Programming 4 March 29th 05 09:07 AM
referencing ComboBox salihyil[_7_] Excel Programming 1 February 26th 04 02:13 PM
referencing ComboBox salihyil[_9_] Excel Programming 0 February 26th 04 09:57 AM


All times are GMT +1. The time now is 09:17 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"