Question : create drop down on excel by vba

hi,

I want to know how to create drop down on excel by vba
like http://office.microsoft.com/en-us/excel-help/create-a-drop-down-list-from-a-range-of-cells-HP005202215.aspx
But all the data are from database , not from excel cells. no data on excel cells that's what I perfered
and also any event on vba when I changed drop down option ? like a1_onclick function

Thank you!
Francis sZE

Answer : create drop down on excel by vba

Hi Francis,
you can archive this by using following code:
All you will have to do to make it work is to add reference to Microsoft 3.x DAO Object Library
(VBA EDITOR >> TOOLS >> REFERENCES)
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
Sub Update_List()

    Dim db As DAO.Database, rst As DAO.Recordset
    Dim path As String
    
    Dim wb As Workbook
    Dim w1 As Worksheet
    Dim qry As String, lst As String
    Dim err As String
    
    Application.DisplayAlerts = False
    Application.ScreenUpdating = False
    
    'Change your path
    path = "C:\Folder\Your.mdb"
    Set db = OpenDatabase(path)

    Set wb = ThisWorkbook
    Set w1 = wb.Worksheets("Sheet1")

    'Change field name and Table name
    qry = "SELECT DISTINCT FieldWithData FROM YourTable;"

    lst = ""
    
    Set rst = db.OpenRecordset(qry)

    With rst
        If .EOF = True And .BOF = True Then
            .MoveFirst
            Do Until .EOF
                lst = lst & .Fields("FieldWithData").Value & ","
                .MoveNext
            Loop
        Else
            'show error if dataset is empty
            err = MsgBox("No data to import", vbOKOnly, "No data to import")
        End If
    End With
    rst.Close
    
    'Change the range where you want your Validation list
    With w1.Range("A1").Validation
        .Delete
        .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:="=" & x
        .IgnoreBlank = True
        .InCellDropdown = True
        .InputTitle = ""
        .ErrorTitle = ""
        .InputMessage = ""
        .ErrorMessage = "Please select from dropdown list."
        .ShowInput = True
        .ShowError = True
    End With
    
End Sub

Private Sub Worksheet_Change(ByVal Target As Range)
'this will run your code every time value of validation field is changed
'Change "$A$1" to any other range  
If Target.Address = "$A$1" Then
        'Do something
    End If
End Sub
Random Solutions  
 
programming4us programming4us