简体   繁体   中英

activex-combobox for name column in excel

I have an excel sheet I use to record casual worker data.

Out of 6 columns, the options to which can be chosen through data validation do not change and are therefore not a problem.(range of days worked/Overtime, etc).

The one thing I need help with is the name column where I would like an activeX combobox so I can make use of autofill and have the ability to add new names all the time.

Unlike data validation which can be dragged and copied down a range of cells (I usually have no more than 100 casual workers), I am unsure how to do this with a combobox so every new row of my name column contains that combobox. Please advise.

If you don't mind using a Form Control ComboBox instead of ActiveX, here is an example I just worked up. I used months instead of names, because I don't know the names you want to use. The concept is the same.

It adds months as list items in the code, although you could do this with an array of values, and a loop instead. For the sake of simplicity of demonstrating programmatically adding ComboBoxes to Cells, I just hard coded the 12 months.

Also, I just populated rows 2 to 20 for the sake of the example.

TESTED:

Sub addComboBoxes()

Dim sheet As String
Dim newName As String
Dim lRow As Long
'Dim lastRow As Long   'Not using for this example.  Worth keeping in mind the option.

    sheet = "Sheet1"   'Set Sheet Name
    'lastRow = Sheets(sheet).Range("A" & Rows.Count).End(xlUp).row    

    For lRow = 2 To 20          'Choose Row Limitations, perhaps 2 To lastRow      
        newName = "cmbAuto" & lRow   

        Set cmbMonthRow = Sheets(sheet).Shapes.AddFormControl _
            (xlDropDown, Left:=Cells(1, 1).Left, Top:=Cells(lRow, 1).Top, Width:=60, Height:=15)

        With cmbMonthRow 
            .ControlFormat.LinkedCell = "A" & lRow
            .ControlFormat.AddItem "January", 1
            .ControlFormat.AddItem "February", 2
            .ControlFormat.AddItem "March", 3
            .ControlFormat.AddItem "April", 4
            .ControlFormat.AddItem "May", 5
            .ControlFormat.AddItem "June", 6
            .ControlFormat.AddItem "July", 7
            .ControlFormat.AddItem "August", 8
            .ControlFormat.AddItem "September", 9
            .ControlFormat.AddItem "October", 10
            .ControlFormat.AddItem "November", 11
            .ControlFormat.AddItem "December", 12

            .ControlFormat.DropDownLines = 12
            .Name = newName
        End With
    Next lRow
End Sub

NOTE: The linked cell returns the index number of the selection. You can have an event for when the value changes, but in the example provided, the linkedCell property is fine.

In this example, I have used months, and since it returns the index, I am putting the value behind the comboBox. I included a screenshot to demonstrate this, and left the column width wide enough to see the value of the cell behind the object. You could of course just have the column width end at the end of the comboBox.

In a cell in Column E, I have a formula using the value of the linked cell:

=IF(A2="","",TEXT(A2*29,"mmmm"))

This returns the month name of the dropdown. Without it, there wouldn't be anything on the sheet actually representing the choice made by the dropdown.
结果

Links:

Office Support: Add a ListBox or ComboBox control to a Worksheet.

Here is an example of a question that I answered recently using a UserForm to do pretty much the same thing, instead of an object in the Sheet, or in this case MANY objects in the sheet. Which is why I prefer to use UserForms. You would have one dropdown that when is changed, the code finds the appropriate cells to manipulate, instead of one for every row.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM