Results 1 to 8 of 8

Thread: VBA in Excel... Combo Box issue

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2003
    Posts
    10

    VBA in Excel... Combo Box issue

    Greetings,

    I've been trying for awhile to have a options appear in a ComboBox in Microsoft Excel by using the following code:

    Private Sub Document_Open()

    ComboBox1.Clear
    ComboBox1.AddItem "Jan"
    ComboBox1.AddItem "Feb"
    ComboBox1.AddItem "Mar"
    ComboBox1.AddItem "April"
    ComboBox1.AddItem "May"
    ComboBox1.AddItem "June"
    ComboBox1.AddItem "July"
    ComboBox1.AddItem "August"
    ComboBox1.AddItem "September"
    ComboBox1.AddItem "October"
    ComboBox1.AddItem "November"
    ComboBox1.AddItem "December"

    End Sub

    This works in Microsoft Word but not in Excel. When opening the document, "Jan" will appear in the Combo Box but nothing else will appear after clicking on the down arrow. Could anyone tell me what I am doing wrong?

    Also, I would ultimately like to have this option pop up asking to pick a month when the worksheet first loads (Prompt the user for the month and then have it fetch data). Is this possible?

    Thanks!
    Dan

  2. #2
    New Member seismicweasel's Avatar
    Join Date
    Jun 2003
    Posts
    5
    the code works for me *if* you change Document_Open() to:

    Worksheet_Activate()

    Worksheet_Activate() is triggered when you select any given sheet in the spreadsheet. If you want to use a more global trigger Workbook_Activate() will trigger when you first open the spreadsheet

  3. #3

    Thread Starter
    New Member
    Join Date
    May 2003
    Posts
    10
    I have tried both Workbook_Activate, Worksheet_Activate, Workbook_Open and Worksheet open. For some reason, nothing is coming up. I don't know if this matters, but when I open the worksheet, the only option that appears is "Jan". When clicking on the down arrow to display the rest of the options in the combo box, nothing appears. I am using Excel 2002.

  4. #4
    New Member seismicweasel's Avatar
    Join Date
    Jun 2003
    Posts
    5
    hmmm, are you writing the code within a module or are you writing the code on the specific sheet?

    you have to place Worksheet_Activate() on the code of the actual sheet you want the code to effect rather than within an modules you have created.

    in the project explorer select the sheet that you want Worksheet_Activate() to work on and enter the code there.

    that should work, probably

  5. #5

    Thread Starter
    New Member
    Join Date
    May 2003
    Posts
    10
    I am writing the code on a specific sheet (Sheet 1) and not for a module. As a test, I tried putting the code in "ThisWorkbook" under all the sheet selections in project explorer but this did not help either.

  6. #6
    Fanatic Member BillBoeBaggins's Avatar
    Join Date
    Jan 2003
    Location
    in your database, dropping your tables.
    Posts
    628
    This worked for me...

    VB Code:
    1. Private Sub Worksheet_Activate()
    2.     ComboBox1.Clear
    3.     ComboBox1.AddItem "Jan"
    4.     ComboBox1.AddItem "Feb"
    5.     ComboBox1.AddItem "Mar"
    6.     ComboBox1.AddItem "April"
    7.     ComboBox1.AddItem "May"
    8.     ComboBox1.AddItem "June"
    9.     ComboBox1.AddItem "July"
    10.     ComboBox1.AddItem "August"
    11.     ComboBox1.AddItem "September"
    12.     ComboBox1.AddItem "October"
    13.     ComboBox1.AddItem "November"
    14.     ComboBox1.AddItem "December"
    15.  
    16. End Sub

    Did you make sure to take the sheet out of design mode?

  7. #7

    Thread Starter
    New Member
    Join Date
    May 2003
    Posts
    10
    Still doesn't work. I tried creating a new workbook also, but that did not help. Every time I test it, I am sure to take it out of design mode.

  8. #8
    Frenzied Member andreys's Avatar
    Join Date
    Sep 2002
    Location
    Los Angeles
    Posts
    1,615
    try this, you may need change sheet name
    VB Code:
    1. Private Sub Worksheet_Activate()
    2.     Sheet1.ComboBox1.Clear
    3.     For i = 1 To 12
    4.         Sheet1.ComboBox1.AddItem MonthName(i)
    5.     Next
    6. End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width