Results 1 to 12 of 12

Thread: [RESOLVED] Excel VBA - Auto Load Combo Box

  1. #1

    Thread Starter
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Resolved [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

  2. #2
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    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.
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  3. #3

    Thread Starter
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    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

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Excel VBA - Auto Load Combo Box

    That's because you are looking at the Sheet events, rather than the Book ones.

  5. #5
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    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.
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  6. #6
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    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 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

  7. #7

    Thread Starter
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    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?

  8. #8
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    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

  9. #9
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    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
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  10. #10

    Thread Starter
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Excel VBA - Auto Load Combo Box

    Thanks guys, it is working fine.

  11. #11
    Lively Member
    Join Date
    Jul 2008
    Posts
    73

    Re: Excel VBA - Auto Load Combo Box

    i am using
    http://www.vbforums.com/showthread.p...Load-Combo-Box
    but not bale to populate the combox

  12. #12
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    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
  •  



Click Here to Expand Forum to Full Width