-5

I have tried these sites for help:

Would appreciate the help!

Here is my code:

Private Sub FusegearPerformanceUserForm1()
    With FailureComboBox
        .AddItem "japp"
    End With
End Sub
ashleedawg
  • 20,365
  • 9
  • 72
  • 105
  • 3
    Uhhhh, sorry, that's not how you ask a question here. For starters, see [ask] and [mcve] and [tour] and [help/on-topic] and [tips](https://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/). You can't expect "free help" from people and then throw them a bunch of links. ***"Here, read this and write me code!"** – ashleedawg Feb 23 '18 at 13:41
  • How do I ask then? – Sebastian Gerbert Feb 23 '18 at 13:42
  • 3
    Please take a [tour] and read [ask]. And asking a question means you need actually to **ask one** not only post a list of links. – Pᴇʜ Feb 23 '18 at 13:42
  • Sorry I'm new. My question is how do I add items to a combobox? The links is what I have tried. Might be me but couldn't make it work. – Sebastian Gerbert Feb 23 '18 at 13:47
  • No, no. A good question is not in a comment (use the [edit] button) and needs more than one line. We cannot help if you don't take some time to explain it detailed and well. Also the code you actually tried needs to be in your question (formatted well as code block) and you need to refer the question to your code. No one here will read 6 links just to see what you did. – Pᴇʜ Feb 23 '18 at 13:48
  • `With FailureComboBox .AddItem "japp" End With` This is the code I tried last time – Sebastian Gerbert Feb 23 '18 at 13:53
  • I told you to edit your original question and not to post comments. Code in comments is unreadable. Please [edit] your question and add everything there including a good question. The more energy you put in your question the more likely you will get a good answer. – Pᴇʜ Feb 23 '18 at 13:54
  • So I don't understand where your issue is, because that is exactly how you add items to a combo box. – Pᴇʜ Feb 23 '18 at 14:00
  • @Pᴇʜ How do you make the code look nice like you do? I tried the " ` " that was suggested in Markdownhelp. aah okay thanks – Sebastian Gerbert Feb 23 '18 at 14:01
  • indent the code 4 spaces to format it as code block (or use the buttons in the editor) the ` is only for inline code between text like `this one` here – Pᴇʜ Feb 23 '18 at 14:02
  • That is why I came here because it doesn't get added at all – Sebastian Gerbert Feb 23 '18 at 14:02
  • make sure you have a combobox named `FailureComboBox` in your form. Add `Option Explicit` at the very top of every form code or module. run the code step-by-step (eg with F8) to debug it. And you need to open the combobox after you added items to see them. – Pᴇʜ Feb 23 '18 at 14:05
  • I renamed it myself in property window and debbuged it several times. What do you mean with "after"? tried `Option Explicit` and it didn't do much for me sadly. – Sebastian Gerbert Feb 23 '18 at 14:09
  • I mean you can only see the added items if you open that combo box. – Pᴇʜ Feb 23 '18 at 14:11
  • Yes I open it. I tried with a new sheet now with all default names. Didn't work either. – Sebastian Gerbert Feb 23 '18 at 14:15
  • 1
    That's impossible. Add a new userform and add a combo box on it. Then add `ComboBox1.AddItem "aaa"` into the `Private Sub UserForm_Activate()` in your form and run that form and there **must** be a `aaa` item in your combobox – Pᴇʜ Feb 23 '18 at 14:18
  • https://imgur.com/a/znCiu This is code and result @Pᴇʜ – Sebastian Gerbert Feb 23 '18 at 15:06
  • @ashleedawg I have cleaned up my question now. Mind taking a look? – Sebastian Gerbert Feb 23 '18 at 15:25
  • @SebastianGerbert - you're persistent, and I believe you do *want* to learn (which is mandatory for this site), so I'll write a quick overview as an answer since it can be confusing at first, but it's important that before you post another question you spend some time reading lots of posts on this site & notice which ones got helpful answers because they included lots of relevant information and showed an effort was made, and which ones didn't. ***Also: make sure you read all the links in my first comment*** to learn the basics of how this site works. ( I'll post in a few minutes.) – ashleedawg Feb 23 '18 at 19:28
  • 2
    I just want to congrat @Pᴇʜ for his/her patience in this issue. I have read all the comments and your behaviour is admirable. This question got more than 14 comments, and still you don't give up. Cheers! – Foxfire And Burns And Burns Feb 23 '18 at 21:13
  • @SebastianGerbert Make sure that your file is NOT saved as `XLSX` when you plan to use macros. Macros can only be saved in `XLSM` (or `XLSB`) format. Try to make sure that the adding macro runs at all. Insert a message box like `MsgBox "I added an item now"` directly after the additem and see if this pops up. This way we find out if the problem is a "macro not running" or `combobox not adding the item` issue. – Pᴇʜ Feb 26 '18 at 07:45
  • 1
    This was the problem, thanks. This raised another question for me tho. I can add items to a combobox now but need to update the combobox for them to show. How do I make them show from the start? – Sebastian Gerbert Feb 26 '18 at 07:53
  • @SebastianGerbert sorry this is not a forum based site (where you can discuss a lot) but a question/answer site (where you ask a significant question and get a significant answer or two). Please do some research work out an example and ask a new well explained question containing all information. Don't forget to add the code you tried. Further questions cannot be answered here in comments. – Pᴇʜ Feb 26 '18 at 07:58

2 Answers2

3

Manipulation of controls in Excel confused me for a while until I realized that there are two kinds of them, and this might be part of your confusion. For example, there is:

  • a Form Control Combo Box, and,

  • an ActiveX Control Combo Box.

The two look, behave, and are controlled similarly, but not identically.

Click image to enlarge: differences between controls


I realize that wasn't your question, but I figured I should make sure you can identify which control you are using, and therefore make sure that you're using (and Googling) the correct information - especially since the terms "Combo Box" and "Drop Down Box" are often used interchangeably. A Google Search for Vcode related to BA Combo Box Control will be wrong 50% of the time, so you need to be more specific.


AS for the code difference:

FORM CONTROL Combo Box

'add item
ActiveWorkbook.Sheets("Sheet1").Shapes("Drop Down 1").ControlFormat.AddItem "abcd"

'remove all items
ActiveWorkbook.Sheets("Sheet1").Shapes("Drop Down 1").ControlFormat.RemoveAllItems


'https://analysistabs.com/vba-code/activex-controls/combobox/

ACTIVEX CONTROL Combo Box

'add item
ActiveWorkbook.Sheets("Sheet1").ComboBox1.AddItem "abcd"

'remove all items
ActiveWorkbook.Sheets("Sheet1").ComboBox1.Clear

More Information:

ashleedawg
  • 20,365
  • 9
  • 72
  • 105
1

So, just for completion:

The solution to this question was, that the file was saved in the XLSX format which cannot contain VBA code. After saving the file in the XLSM format everything worked as expected.

Pᴇʜ
  • 56,719
  • 10
  • 49
  • 73