Results 1 to 10 of 10

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

  1. #1
    Super Moderator Hack's Avatar
    Join Date
    Aug 01
    Location
    Searching for mendhak
    Posts
    58,283

    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
    Please use [Code]your code goes in here[/Code] tags when posting code.
    When you have received an answer to your question, please mark it as resolved using the Thread Tools menu.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    I dont answer coding questions via PM or EMail. Please post a thread in the appropriate forum section.

    Creating A Wizard In VB.NET
    Paging A Recordset
    What is wrong with using On Error Resume Next
    Good Article: Language Enhancements In Visual Basic 2010
    Upgrading VB6 Code To VB.NET
    Microsoft MVP 2005/2006/2007/2008/2009/2010/2011/2012/Defrocked

  2. #2
    Head Hunted anhn's Avatar
    Join Date
    Aug 07
    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
    Super Moderator Hack's Avatar
    Join Date
    Aug 01
    Location
    Searching for mendhak
    Posts
    58,283

    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
    Please use [Code]your code goes in here[/Code] tags when posting code.
    When you have received an answer to your question, please mark it as resolved using the Thread Tools menu.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    I dont answer coding questions via PM or EMail. Please post a thread in the appropriate forum section.

    Creating A Wizard In VB.NET
    Paging A Recordset
    What is wrong with using On Error Resume Next
    Good Article: Language Enhancements In Visual Basic 2010
    Upgrading VB6 Code To VB.NET
    Microsoft MVP 2005/2006/2007/2008/2009/2010/2011/2012/Defrocked

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 02
    Location
    Bristol, UK
    Posts
    35,564

    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 07
    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
    Super Moderator koolsid's Avatar
    Join Date
    Feb 05
    Location
    Mumbai, India
    Posts
    11,415

    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

    Microsoft MVP: 2011 - Till Date IMP Links : Acceptable Use Policy, FAQ

    MyGear:
    Sony VGN-FZ27G with a triple boot between (XP+Office 2003+VB6), (VISTA+Office 2007+VS2008) and (Win7+Office 2010+VS2010) || Sony VPCCB-45FN with a Win7+Office 2010+VS2010. VM: (XP+Office 2003+VB6), (VISTA+Office 2007+VS2008), (Win8+Office 2010+VS2012) || Mac Book Pro (10.6.8) with Office 2011

  7. #7
    Super Moderator Hack's Avatar
    Join Date
    Aug 01
    Location
    Searching for mendhak
    Posts
    58,283

    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?
    Please use [Code]your code goes in here[/Code] tags when posting code.
    When you have received an answer to your question, please mark it as resolved using the Thread Tools menu.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    I dont answer coding questions via PM or EMail. Please post a thread in the appropriate forum section.

    Creating A Wizard In VB.NET
    Paging A Recordset
    What is wrong with using On Error Resume Next
    Good Article: Language Enhancements In Visual Basic 2010
    Upgrading VB6 Code To VB.NET
    Microsoft MVP 2005/2006/2007/2008/2009/2010/2011/2012/Defrocked

  8. #8
    Super Moderator koolsid's Avatar
    Join Date
    Feb 05
    Location
    Mumbai, India
    Posts
    11,415

    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

    Microsoft MVP: 2011 - Till Date IMP Links : Acceptable Use Policy, FAQ

    MyGear:
    Sony VGN-FZ27G with a triple boot between (XP+Office 2003+VB6), (VISTA+Office 2007+VS2008) and (Win7+Office 2010+VS2010) || Sony VPCCB-45FN with a Win7+Office 2010+VS2010. VM: (XP+Office 2003+VB6), (VISTA+Office 2007+VS2008), (Win8+Office 2010+VS2012) || Mac Book Pro (10.6.8) with Office 2011

  9. #9
    Head Hunted anhn's Avatar
    Join Date
    Aug 07
    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
    Super Moderator Hack's Avatar
    Join Date
    Aug 01
    Location
    Searching for mendhak
    Posts
    58,283

    Re: Excel VBA - Auto Load Combo Box

    Thanks guys, it is working fine.
    Please use [Code]your code goes in here[/Code] tags when posting code.
    When you have received an answer to your question, please mark it as resolved using the Thread Tools menu.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    I dont answer coding questions via PM or EMail. Please post a thread in the appropriate forum section.

    Creating A Wizard In VB.NET
    Paging A Recordset
    What is wrong with using On Error Resume Next
    Good Article: Language Enhancements In Visual Basic 2010
    Upgrading VB6 Code To VB.NET
    Microsoft MVP 2005/2006/2007/2008/2009/2010/2011/2012/Defrocked

Posting Permissions

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