Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

building dropdown lists dynamically?

I need to build a drop down list dynamically, whereby after entering particular text into a cell I then execute some SQL and build a Dropdown from the returned rows.

How is the event concentrated on the value of just one cell (rather than the whole spreadsheet) done?

Must I "paste" the SQL row values onto a spreadsheet before I create the Dropdown? Is it possible in VBA to populate the Dropdown without having to paste values onto a spreadsheet and then highlight them to create the Dropdown?

Thanks

like image 745
mezamorphic Avatar asked Jun 13 '12 10:06

mezamorphic


People also ask

How do you create a dependent cascading drop-down list?

In the same or in another spreadsheet, select a cell or several cells in which you want your primary drop-down list to appear. Go to the Data tab, click Data Validation and set up a drop-down list based on a named range in the usual way by selecting List under Allow and entering the range name in the Source box.


1 Answers

No it is not necessary to paste values in the sheet to create the dropdown. See this example

Option Explicit

Sub Sample()
    Dim dvList As String

    '~~> You can construct this list from your database
    dvList = "Option1, Option2, Option3"

    '~~> Creates the list in Sheet1, A1
    With Sheets("Sheet1").Range("A1").Validation
        .Delete
        .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
        xlBetween, Formula1:=dvList
        .IgnoreBlank = True
        .InCellDropdown = True
        .InputTitle = ""
        .ErrorTitle = ""
        .InputMessage = ""
        .ErrorMessage = ""
        .ShowInput = True
        .ShowError = True
    End With
End Sub
like image 92
Siddharth Rout Avatar answered Sep 16 '22 12:09

Siddharth Rout