Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Excel combobox listfillrange property pointing at a formula-based named range has issues

Tags:

ActiveX combobox objects in Excel do not behave well when their ListFillRange refers to a formula-based Named Range (Defined Name).

I think I have encountered other errors and possibly even Excel crashes thanks to this, but right now all that happens is the combobox_change() event is triggered anytime ANY cell in the workbook is changed.

I am not sure if this is really a bug, or if there is a fix, or a workaround. If it is a bug, how do I report it to the Excel people?

And finally, the real meat of my question is "How do I work around this issue best?" I would like to have some formula-based named ranges, but it seems like this won't be possible.

To reproduce this bug, do the following:

  1. Create a new workbook. On Sheet3, create a small table 3 columns across, and several rows high.
  2. Create a named range with this formula (or an equivalent): =OFFSET(Sheet3!$A$2:$C$36,0,0,COUNTA(Sheet3!$A:$A),COUNTA(Sheet3!$4:$4)) To do this use Input>Name>Define. Name the range something like "demoRange"
  3. Go to Sheet1 and create a combobox, (it must be on a separate sheet). (Use the Control Toolbox menu, not the Forms menu).
  4. Click on the Design Mode button (the blue triangle with pencil), then right click on the combo box and go to Properties.
  5. In the properties window for the combobox, change the ListFillRange property so that it points at the named range you created in step 2 ("demoRange").
  6. You may want to change the ColumnCount property to 3, and the ColumnWidths property to "50,50,50"
  7. Set the linkedCell property to cell "A1" by typing A1 in the linkedCell property.
  8. Close the properties window, and double click on the combobox to define its change() event.
  9. Put a Debug.Assert(false) or Msgbox("demo") line in the subroutine for the new combobox's change event.
  10. Exit design mode
  11. important - Now select an item in the combobox. The event should trigger normally the first time. (The bug will not show if you don't do this step--something must be selected in the combobox)
  12. Edit cells anywhere in the workbook [Edit] or any other open workbook [/edit], on any sheet and any location. Each time you edit any cell, (at least for me), the onchange event for the combo box is run.

Again, is this normal, and what is the best alternative for what I am doing? This combo box gets linked to various cells, and is supposed to be a replacement for the tiny font in the data validation dropdowns excel provides by default.

like image 888
Kimball Robinson Avatar asked Aug 11 '09 22:08

Kimball Robinson


People also ask

How do I link a cell to a combobox in Excel VBA?

Format a Form Control combo boxRight-click the combo box and pick Format Control. Input range: Type the range of cells containing the list of items. Cell link: The combo box can be linked to a cell where the item number is displayed when you select an item from the list.

How do I activate a combobox in Excel?

To add or edit the Combobox, follow these steps: On the Ribbon, click the Developer tab. If you do not see the Developer tab, follow the steps here to show it.) Click Insert, and under ActiveX Controls, click on the Combo box button, to activate that tool.


1 Answers

My advice is to never use ListFillRange and LinkedCell. They are just trouble. Fill your listbox with List and use the Change event to write to the cell. Somewhere, maybe the Workbook_Open event, fill the listbox

Private Sub Workbook_Open()

    Sheet2.ListBox1.Clear
    Sheet2.ListBox1.List = Sheet1.Range("demoRange").Value

End Sub

Then in the change event in the Sheet2 module, check that something was clicked and write it to the cell

Private Sub ListBox1_Change()

    If Me.ListBox1.ListIndex >= 0 Then
        Sheet2.Range("A1").Value = Me.ListBox1.Value
    End If

End Sub
like image 153
Dick Kusleika Avatar answered Oct 12 '22 03:10

Dick Kusleika