Results 1 to 16 of 16

Thread: Combobox in a worksheet

  1. #1

    Thread Starter
    Hyperactive Member dRAMmer's Avatar
    Join Date
    Oct 2001
    Location
    strangelans
    Posts
    463

    Resolved Combobox in a worksheet

    Hi to all!

    I've been searching on this forum topic for what I want to create for almost an hour now. All I found is using a combox with a userform, what I want is to create/include a combox into a worksheet. It's like when you have a list created from "Data->List->Create List.." menu. This combox will contain data coming from sql server.

    Thanks in advance!
    Last edited by dRAMmer; Mar 1st, 2006 at 08:01 PM.
    live, code and die...

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Combobox in a worksheet

    Wehn ever you need to know how to do something in VBA, just record a macro doing your needed task and then view the Module code generated. Although, I dont really recommend using the ActiveCell object but you get the idea.
    VB Code:
    1. Sub Macro1()
    2. '
    3. ' Macro1 Macro
    4. ' Macro recorded 2/13/2006 by VB/Office Guru
    5.     ActiveSheet.ListObjects.Add(xlSrcRange, Range("$A$1"), , xlNo).Name = "List1"
    6.     Range("A2").Select
    7.     ActiveCell.FormulaR1C1 = "test"
    8.     Range("A3").Select
    9.     ActiveCell.FormulaR1C1 = "red"
    10.     Range("A4").Select
    11.     ActiveCell.FormulaR1C1 = "blue"
    12.     Range("A5").Select
    13.     ActiveCell.FormulaR1C1 = "ttt"
    14.     Range("A6").Select
    15.     ActiveCell.FormulaR1C1 = "aaa"
    16.     Range("A1:A6").Sort Key1:=Range("A1"), Order1:=xlAscending, Header:= _
    17.         xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
    18.         DataOption1:=xlSortNormal
    19. End Sub
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    Hyperactive Member dRAMmer's Avatar
    Join Date
    Oct 2001
    Location
    strangelans
    Posts
    463

    Re: Combobox in a worksheet

    Hi Rob!

    Thanks for the quick reply. I have done already trying to see if I can make something out of it. But my problem remains that I can't create a "dynamic" combox out of it. If try to create it into a list then the connection will be lost. Here is what I'm trying to do:

    - Create a combo box on a sheet
    - Populate it with data from a select statement
    - Have another combobox whose values depends or is filtered out by the value given by the first combobox. values will also be coming from database.

    Can you give an example about this.
    live, code and die...

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Combobox in a worksheet

    i think so. Does the combo's need to be like the filtering or sorting ones or can it be an ActiveX combo box?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5

    Thread Starter
    Hyperactive Member dRAMmer's Avatar
    Join Date
    Oct 2001
    Location
    strangelans
    Posts
    463

    Re: Combobox in a worksheet

    As much as possible I'd like to shy off from activeX, you see I have something similar done w/Smart Doc. But as you know, smart docs are hard to deploy. So I want a simple one, where users can just open the workbook and start typing.

    The control is more of a filtering type.
    live, code and die...

  6. #6

    Thread Starter
    Hyperactive Member dRAMmer's Avatar
    Join Date
    Oct 2001
    Location
    strangelans
    Posts
    463

    Re: Combobox in a worksheet

    Anybody there who can help? I would really appreciate it. Thanks in advance!
    live, code and die...

  7. #7
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Combobox in a worksheet

    Sorry for the delay, dont worry about the ActiveX combo box as its part of VBA so no other dependancies are needed that Excel doesnt already need.

    I suggest using that one since its so similar to the VB 6 Combo box control.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  8. #8

    Thread Starter
    Hyperactive Member dRAMmer's Avatar
    Join Date
    Oct 2001
    Location
    strangelans
    Posts
    463

    Re: Combobox in a worksheet

    Thanks for the reply, how about the sample code I was asking, can you give me a code I can start with. Thanks again.
    live, code and die...

  9. #9
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Combobox in a worksheet

    Adds 10 items to a ComboBox1 on Sheet1.
    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Workbook_Open()
    4.     Dim i As Integer
    5.     For i = 1 To 10
    6.         Workbooks(1).Sheets("Sheet1").OLEObjects("ComboBox1").Object.AddItem "Item " & i
    7.     Next
    8. End Sub
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  10. #10

    Thread Starter
    Hyperactive Member dRAMmer's Avatar
    Join Date
    Oct 2001
    Location
    strangelans
    Posts
    463

    Re: Combobox in a worksheet

    I have managed to insert a combobox on the sheet then pasted your code on Workbook_Open event and got this error: Application-defined or object-defined error
    live, code and die...

  11. #11
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Combobox in a worksheet

    Does your combo box reside on "Sheet1" and the cbo is named "ComboBox1"?
    Also, macros enabled?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  12. #12

    Thread Starter
    Hyperactive Member dRAMmer's Avatar
    Join Date
    Oct 2001
    Location
    strangelans
    Posts
    463

    Resolved Re: Combobox in a worksheet

    Hi Rob!

    Yes is the answer to all your question. I had solved it now and I'd like to know why do I have to save the workbook first to be able not to get that error? I'd prefer a technical explanation about that, something like an answer from "excel documentation".

    Thanks so much
    live, code and die...

  13. #13
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Combobox in a worksheet

    I just pasted my code into a new workbook and added a cbo then ran the workbook_open procedure and it populated just fine.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  14. #14

    Thread Starter
    Hyperactive Member dRAMmer's Avatar
    Join Date
    Oct 2001
    Location
    strangelans
    Posts
    463

    Re: Combobox in a worksheet

    I've created a new workbook wrote a simple debug.print code in Workbook_Open and nothing is happening, I really have to save it first for the code to run. Anyway, thanks a lot, you've been a great help, problem solved!
    live, code and die...

  15. #15
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: Combobox in a worksheet

    Although this is resolved; as an alternate you could use "Create List" for a select range, then dynamically add values. No CBO overhead. - Just a thought

  16. #16
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Combobox in a worksheet

    Workbook_Open only runs upon the opening or a workbook. If you manually run my code in post #9 on a new unsaved workbook with a combo it populates it just fine. Maybe a bit more of when you need it populated?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

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