Link to home
Start Free TrialLog in
Avatar of kerikeri
kerikeriFlag for New Zealand

asked on

Populate a cell value from a dropdown list

I need to limit the selection of road names.  Thought I would use a dropdown list, but this is quite inadequate.  See simple file where the list of valid roads is in a separate worksheet.  I've also tried using a combo box but can't see how I would link this to a column.  So easy to do in Access!  Hopefully it's just my inexperience with EXCEL...
EEDropdownList.xlsx
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

in C1, you need to put in the Data Validation so that it can refers the value from your list of values.
to do that, first go to worksheet: Roads, create a Name.

User generated image
now go back to worksheet: MainSheet, select cell C1, then go to tab: Data > Data Validation and add this.

User generated image
so now you should able to see a dropdown then.

User generated imageEEDropdownList_b.xlsx
In this instance, you might be better off with a Controls Combobox, not a Forms Combobox which you are currently using. Doing so allows the following pertinent properties...

ListFillRange - The range of cells to populate the combobox. Can be multiple columns wide.
BoundColumn - The column from ListFillRange which  returns the selected value. The value isn't necessarily the displayed text.
LinkedCell - The cell which displays the selected value of the combobox.

See the attached example. To view the properties, click Design Mode on the Developers tab, right-click the control and select Properties.
EEDropdownList.xlsx
I think you need a searchable drop down list which gets populated with matching roads as you type.
Please refer to the attached and see if this is what you were trying to achieve.
SearchableCombobox.xlsm
Avatar of kerikeri

ASKER

Thank you for your quick responses.  Ryan's was the same solution as what my original description was, not really a solution for me.  Wayne's seemed possible, but I couldn't figure out how the LinkedCell requirement could be defined to a relative cell and as I'm using the logic in a data entry form, that doesn't work.  It has to be able to populate a different linked cell for each row .  So Neeraj's is the one that I'm using.  The only thing not so good is it doesn't include the complete list of roads on initially opening.  However, it's definitely a step in the right direction.  Thanks again all of you.
I does show the complete list when activated after selecting a cell in column A.
Watch this short video.
https://www.screencast.com/t/nGbNbccB12Y
Maybe I shouldn't have closed this so quickly.   Hope you don't mind answering further questions about this solution.
SampleSpreadsheet.xlsm
EEComboBox-Question-.docx
but I couldn't figure out how the LinkedCell requirement could be defined to a relative cell

If added manually, you need to set the LinkedCell property of each combobox individually. Alternatively, you can use the below macro to add the combobox to the current selection of cells. It sets the LinkedCell to the cell to the immediate right of the cell the combobox is in.

Sub AddComboboxes()
        
    Dim cell As Range, cb As OLEObject
    For Each cell In Selection
        Set cb = ActiveSheet.OLEObjects.Add(ClassType:="Forms.ComboBox.1", Link:=False, _
        DisplayAsIcon:=False, Left:=cell.Left, Top:=cell.Top, Width:=cell.Width, Height:=cell.Height)
        With cb
            .LinkedCell = cell.Offset(0, 1).Address
            .Object.BoundColumn = 2
            .ListFillRange = "Roads!A2:B786"
        End With
    Next
    
End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Subodh Tiwari (Neeraj)
Subodh Tiwari (Neeraj)
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you for your latest comment Neeraj - I need to apply this to the spreadsheet attached in my last comment:  "The combobox is hidden and sitting in A1 and it appears only when you select any cell in column A which has a data validation and once you click outside the column A, it disappears again. So there is only one combobox sitting on the sheet and it moves to the cell you select in column A. Also the combobox is linked to the selected cell dynamically by the code."   I'm trying to replicate this combo box in my spreadsheet so would like to keep this question open until I've managed to do this.  Thanks, it's one part of my quite busy job right now!
thank you