Can't add items to a combobox - vba

I have tried these sites for help:
excel-easy.com
sitestory.dk
ozgrid.com
stackoverflow.com
contextures.com
microsoft.com
Would appreciate the help!
Here is my code:
Private Sub FusegearPerformanceUserForm1()
With FailureComboBox
.AddItem "japp"
End With
End Sub

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:
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:
Different Types of Combo Boxes Explained
VBA ComboBox
MSDN : VBA Shape Members

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.

Related

Ellipsis Textbox for VBA Userform File Select

I am trying to create a path selection user interface for an extensive VBA program I've been working on, but I can't seem to get the ellipsis textbox that I'd like. This is a very common feature, especially in option tables. This is an example of what I'd like to get, straight from the VBA Options panel:
I would LOVE to find a way to get the same functionality in a Userform. The only solution that I've found thus far is to use a combo box with the ellipsis arrow option enabled. However, there doesn't seem to be an apparent way to use the activation of the combo box arrow to run a dialog box, nor does there seem to be a way to make it look UNLIKE a combo box. Last resort I use a button below the text box, but I'd really prefer a less-bulky way of doing this.
Any solution would be greatly appreciated.
The only solution that I've found thus far is to use a combo box with
the ellipsis arrow option enabled. However, there doesn't seem to be
an apparent way to use the activation of the combo box arrow to run a
dialog box, nor does there seem to be a way to make it look UNLIKE a
combo box
Your suggestion does work, and it is surely less complex and more elegant than having two controls work together, Button + Textbox.
A Combo can achieve perfectly the desired feature, in the following way.
1) In design mode, set the button style to Ellipsis
DropButtonStyle = fmDropButtonStyleEllipsis
And eventually, make the ellipsis show up only when the combo is entered, by setting the design-time property:
ShowDropButtonWhen = ShowDropButtonWhenFocus
2) If needed, you can set other design-time properties to have some look and feel. The defaults look pretty good however.
3) Add the following handler to the parent userform. The snippet simulates the launching of a dialog and getting a new value or cancelling. It does not show any dropdown window. (but you still have control over that: if you want to show it according to some condition, you still can call the method ComboBox1.DropDown)
Private Sub ComboBox1_DropButtonClick()
' The following two lines avoid to call the routine twice, at entry and at exit
Static i As Integer
i = (i + 1) Mod 2: If i = 0 Then Exit Sub
With ComboBox1
s = InputBox("enter some text", , .Value) '<~~ simulates any dialog
If s <> "" Then .Value = s
SendKeys ("{Enter}") '<~~ to close immediately the dropdown window
End With
End Sub
Try it ;)
Not only do ComboBoxes have Drop Buttons, so do TextBoxes (as do Excel's RefEdit controls). Even though you can't access the Textbox's Drop Button at design time, you can do so at runtime. Using a textbox avoids having to deal with the dropped down list of a combobox.
Given a textbox named TextBox1, the following code will provide the desired ellipsis drop button:
Private Sub UserForm_Initialize()
With Me.TextBox1
.DropButtonStyle = fmDropButtonStyleEllipsis
.ShowDropButtonWhen = fmShowDropButtonWhenAlways
End With
End Sub
Then use the DropButtonClick event of the textbox to perform whatever action you want:
Private Sub TextBox1_DropButtonClick()
'' Code here to do what you need
End Sub
I have an extensive example at Alternative to Excel’s Flaky RefEdit Control that uses a textbox with a "Reduce" drop button to replicate the functionality of Excel's unreliable RefEdit controls.

Excel: Object Text Box can't do Carriage Returns in Protected vs. Unprotected Sheet states (w/ Text Unlocked)...why?

First and foremost, my due diligence rounded up a ton of answers regarding ActiveX Text Boxes, but nothing really about Object Text Boxes. For my project, I cannot use any ActiveX.
OK, so when a Sheet is Unprotected, an Object Text Box (from Insert > Shapes) works pretty much the way I want it to: the most important thing being that I can hit the Enter key and get a carriage return. Then, I go into the Shape Properties, and uncheck Lock Text, and protect the sheet.
Once the sheet is protected, though, the ability to do carriage returns (type Enter, and go down one line) goes away. Shift+Enter and Alt+Enter are no-goes as well.
Is it just not possible to have this functionality available? Are there any workarounds? Why does Excel hate me? Here are some of my ideas:
Unprotect Sheet when Text Box is clicked/activated, Protect when not
(couldn't figure out the syntax in VBA for this. "If Intersect..." is what I'm thinking)
Insert Word Doc Object (don't like this because one-click enters the
formula bar editing, and I can't get the font to stay)
Just use a merged cell and instruct users to double-click to enter
and use Alt+Enter for a new line.
The winning option for now is using a merged cell, but I may just have to see if ActiveX will work on our network. I really want to stick to the KISS principle here if at all possible for the end user...I don't mind coding in the backend to make it work, though.
Thank you for your thoughts!
EDIT: Here's some images to help...
Here's the functionality that I would like to have when my Sheet is protected:
Next, this is an ActiveX text box with it's properties window displayed (Developer > Design Mode > Properties). The properties that make it somewhat usable when the Sheet is protected are circled in red, the Multiline and Enter Key Behavior. But again, I'd prefer to not have to use ActiveX...plus, the user cannot change font color by line.
Finally, I found this interesting: There is another Text box under Form Control that is grayed out. From a search, it looks like this was taken away in favor of the drawing objects version of the text box...or maybe it's the same? The left is the drawing objects one, the middle is the grayed out Form Control, and the right is the ActiveX.
In sum, I would just like to see if there is a way to have the functionality of an unprotected Sheet's Shapes Text Box when the sheet is protected.
Assuming you are working with a TextBox shape, inserted from the ribbon, here:
Then you can use the optional parameter in the Protect method:
Sheet1.Protect DrawingObjects:=False
This will allow the user to edit text boxes on the worksheet, but the sheet itself will remain protected.
If you are using a Form Control (inserted from the Develper/Design ribbon) then you can set the .MultiLine property by accessing the shape's OLEFormat.Object:
Sub test()
Dim tb As Shape
Dim x As Object
'Get a handle on the SHAPE
Set tb = ActiveSheet.Shapes(2)
'You have to access its properties from the OLEFormat.Object:
tb.OLEFormat.Object.Object.MultiLine = True
End Sub
In my test, even on a Protected worksheet this allows the user to Shift + Enter to insert carriage returns:
Ctrl + Shift + Enter seems to work on a protected sheet.

Excel VBA Userform - How do I add a value to a combo box from a list in the userform?

Is there a way to do the following:?
I would like a manual text box that when filled in and the user clicks "Add" and it adds the value to the combo box. However, I will also have this list stored somewhere so that the next time the user loads the form they can select "Previously Used" items from a list and these will then also be added to the combo box.
I will be adding a button to "Add all" of the previous items but it must still have the option to add items manually each time.
Even more useful would be to have an option to check items in and out of the combo box as well as the manually text input option.
I tried to add an image I found to better explain this but I need 10 rep points. Therefore, I hope this makes sense but please feel free to comment for more details and a better explanation if needed.
Link to an example of what I am thinking of but without the manual addition field:
http://kb.blackboard.com/download/attachments/14057766/bbui_multi_select.gif?version=1&modificationDate=1202823953747
Use this
combobox1.additem txtValue.Text
Sheets("SheetName").Cells(lastRow+1,col).value=txtValue.Text
Also add below code in Form_activate
For i=1 to lastrow
combobox1.additem Sheets("SheetName").Cells(i,col).value
next i

How to pass the value (true/false) of checkboxes created run-time on a userform

I am trying to create a number of checkboxes on a UserForm after reading all the non-empty rows in an excel sheet. That means these checkboxes have to be created in run-time. I also want to put a CommandButton on the UserForm. What I want is that once the user presses this CommandButton, the code should be able to send to a subroutine the information on which checkboxes are checked and what their names are.
Could anyone help me with problem.
Instead of trying to dynamically create checkboxes on a userform (which I'm not even sure is possible) consider using a listbox with a ListStyle of fmListStyleOption and with MultiSelect turned on with fmMultiSelectMulti
Populate the Listbox using the AddItem Method
For i = 0 to 9
Me.lbxDivisions.AddItem
Me.lbxDivisions.List(i) = "Checkbox " & format(i)
Next i
And determine which items are checked via the Selected property:
For i = 0 To lbxDivisions.ListCount - 1
If lbxDivisions.Selected(i) Then
MsgBox "Item " & Format(i) & " is selected and has value " & lbxDivisions.List(i)
End If
Next i
You can programmatically add form controls (check boxes, listboxes, etc) to userforms. From within the form's code module,
Me.Controls.Add "Forms.CheckBox.1", "CheckBox1", True)
From any other code module, just reference the form by name, instead of Me, e.g.,
MyUserForm.Controls.Add "Forms.CheckBox.1", "CheckBox1", True)
Personally I would favor using a more dynamic control (like a list box or combobox) unless your task absolutely requires you to use check boxes. With dynamic controls you need to manage their size, location relative to other controls, resize the userform (if necessary), etc., and although it's kind of possible to add event handling to these controls (see here), that's really limited (e.g., if you expect you need to add 10 check boxes each of which do a different thing, you need to pre-write 10 check box subroutines. If you create 11 check boxes but only 10 pre-written routines, the last check box won't do anything. It would be easier to just create all the check boxes when designing the form, and then programmatically set them to Visible=True or Visible=False as circumstance requires.
So, I'd favor using a dynamic control like a listbox or combobox, but it is possible to add form controls like checkboxes at run-time, if you must.

VBA Excel comboBox dropdown is empty after obj.addItem

I have an Excel2010 VBA userform that has one comboBox, from which the user should be able to select a currently-open Excel Workbook. The USERFORM_Initialize subroutine contains (among other things) :
cbWorkbook.Clear
cbWorkbook.Visible = True
For Each wb In Workbooks
cbWorkbook.AddItem wb.name
Next wb
I have set a breakpoint at this code, and am able to step through it; in the present situation there are four open workbooks, and the "for each" is iterated four times, as appropriate. And I can see that wb.name contains the values that I want.
However, when the form displays and the dropbox arrow is clicked, the "list" is empty. It looks like there is room for one item, and that item is blank. (I believe this is typical of an empty dropdown box.)
Select attributes for the combobox are:
Autosize=False; AutoTab=false; Enabled=True; DropButtonStyle=1-fmDropButtonStyleArrow;
Height=18; ListRows=8; ListStyle=0; Locked=False; ShowOptionWhen=2; SpecialEffect=2; Style=0; Visible=True. At the time of execution, cbWorkbook.listCount = 4
This is in development, and it did appear to work as expected yesterday, but now seems to never work. Any ideas where I might be going wrong?
EDIT: I found the solution to this: I had inadvertantly duplicated another combo box over the top of cbWorksheet, effectively hiding it. The control I was seeing was empty, while the control I wanted was overlaid. Deletion of the rogue control box solved the issues.
My apologies; this should have been the first thing I sought.
I found the solution to this: I had inadvertantly duplicated another combo box over the top of cbWorksheet, effectively hiding it. The control I was seeing was empty, while the control I wanted was overlaid. Deletion of the rogue control box solved the issues.
My apologies; this should have been the first thing I sought.
With ComboBox1
.AddItem "This"
.AddItem "Is"
.AddItem "A"
.AddItem "Test"
End With
or if you want to fill it with Range data:
ActiveSheet.Shapes("ComboBox1").Select
Selection.ListFillRange = "k1:k10"
PS - submit your file for review; it should be easier to look at!