+ Reply to Thread
Results 1 to 10 of 10

VBA Combobox in excel sheet

  1. #1
    Registered User
    Join Date
    07-24-2009
    Location
    London,England
    MS-Off Ver
    Excel 2003
    Posts
    6

    VBA Combobox in excel sheet

    Hi, I'm new to this forum and new to VBA, I have a small question that I need a little help with.

    I'm using excel 2003.

    basically what I'm trying to do is create a combobox on sheet 1 of my workbook.

    the code I've used is...


    Private Sub ComboBox1_DropButtonClick()
    With ComboBox1
    ComboBox1.AddItem "aaa"
    ComboBox1.AddItem "bbb"
    ComboBox1.AddItem "ccc"
    End With
    End Sub


    this does work, but each time I click on the combo box it repeats the list over and over again for each time I clikc on the list.

    hope this is making sense.

    thanks for any help in advance.

    regards
    Sukh

  2. #2
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,998

    Re: VBA Combobox in excel sheet

    Hi,
    You can use:
    Please Login or Register  to view this content.
    to clear the list before populating it.
    Everyone who confuses correlation and causation ends up dead.

  3. #3
    Registered User
    Join Date
    07-24-2009
    Location
    London,England
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: VBA Combobox in excel sheet

    Hi, sorry I've been away for a while, I will try this.

    thanks for your help

    regards
    Sukh

  4. #4
    Registered User
    Join Date
    07-24-2009
    Location
    London,England
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: VBA Combobox in excel sheet

    Sorry if I'm being a little slow

    my code now looks like

    Private Sub ComboBox1_DropButtonClick()

    With ComboBox1

    ComboBox1.Clear

    ComboBox1.AddItem "aaa"
    ComboBox1.AddItem "bbb"
    ComboBox1.AddItem "ccc"

    End With

    End Sub


    but now each time I select a entry it doesnt display in the combo box.

    any help is much apprectaied

  5. #5
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,998

    Re: VBA Combobox in excel sheet

    Sorry, I didn't notice that you were using the dropbuttonclick event. That's not a good event to populate the list from - I would suggest you use the worksheet_activate event and/or the workbook_open event.

  6. #6
    Registered User
    Join Date
    07-24-2009
    Location
    London,England
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: VBA Combobox in excel sheet

    Ok, I've done this now,

    in the sheet 1 code window I have the following code

    Private Sub Worksheet_Activate()

    Sheets("sheet1").Select


    With ComboBox1

    ComboBox1.Clear

    ComboBox1.AddItem "aaa"
    ComboBox1.AddItem "bbb"
    ComboBox1.AddItem "ccc"

    End With

    End Sub


    when I open the workbook, it doesnt activate the combo box, I have to click on sheet2 tab in excel and then sheet 1 again and then the combo box works.

    I tried activating the worksheet in workbook open like...

    Private Sub Workbook_Open()

    Sheets("sheet1").Select

    End Sub


    but this doesnt activate the combo box either, sorry if im being slow :s

  7. #7
    Valued Forum Contributor mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,983

    Re: VBA Combobox in excel sheet

    SukhiRehal

    Please take a couple of minutes and read ALL theForum Rules then wrap your VBA code (Rule 3)
    Please Read Forum Rules Before Posting
    Wrap VBA code by selecting the code and clicking the # icon or Read This
    How To Cross Post politely

    Top Excel links for beginners to Experts

    If you are pleased with a member's answer then use the Scales icon to rate it
    If my reply has assisted or failed to assist you I welcome your Feedback.

  8. #8
    Registered User
    Join Date
    07-24-2009
    Location
    London,England
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: VBA Combobox in excel sheet

    Apologies I hope this is the correct way of posting

    The code for sheet 1.

    Please Login or Register  to view this content.
    The code for ThisWorkBook

    Please Login or Register  to view this content.
    but sheet 1 doesnt seem to automatically activate, any suggestions?

    thanks

  9. #9
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,998

    Re: VBA Combobox in excel sheet

    Try this in the Open event:
    Please Login or Register  to view this content.

  10. #10
    Registered User
    Join Date
    07-24-2009
    Location
    London,England
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: VBA Combobox in excel sheet

    ahh man, thankyou it worked.

    much appreciated.

    regards
    Sukh

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1