Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use VBA to select the first iterm in a drop down list in Excel

Tags:

excel

vba

I used Data Validation to create some drop down lists. Anyone know how to use VBA to select the first iterm in a drop down list?

I tried 'Split' function:

cell.Value = Split(cell.Validation.Formula1, ",")(0)

but it did not work well, it will only work if I put like "option1, option2" in the source in Data Validation window. If I refer the source to a range of options, then it will return with errors.

I guess there should some smarter ways.

Thanks!

like image 751
Xiao Qiang Avatar asked Aug 08 '13 12:08

Xiao Qiang


People also ask

How do I select Data from a drop-down list in Excel?

On the worksheet where you applied the drop-down list, select a cell that has the drop-down list. Go to Data > Data Validation. On the Settings tab, click in the Source box, and then on the worksheet that has the entries for your drop-down list, Select cell contents in Excel containing those entries.

Which VBA control works as a drop-down list?

Create drop down lists in a UserForm, by using the ComboBox control. In this example, there are two ComboBox controls, one for Part ID, and one for Location.


1 Answers

Sub test()
    Dim adr As String
    With Range("c4")
        adr = Mid(.Validation.Formula1, 2)
        Debug.Print Range(adr).Cells(1, 1)
    End With
End Sub

so your answer is:

set c = range("c4")
c.Value = Range(Mid(c.Validation.Formula1, 2)).Cells(1, 1).Value

like image 70
iDevlop Avatar answered Oct 12 '22 11:10

iDevlop