Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Edit or export drop down lists in Excel

Working on an existing spreadsheet which contains two dropdown lists. Is it possible to export or edit data from these drop lists?

like image 849
a coder Avatar asked Dec 03 '25 18:12

a coder


1 Answers

In a Data Validation list, you can have these 3 scenarios.

A) A formula which refers to a range in the same sheet

enter image description here

B) A List which is directly typed using commas

enter image description here

C) A formula which refers to a named range from same/another sheet

enter image description here

Now we need to cater to all the three scenarios in case we want to retrieve that list.

To access the list of a Data Validation Cell, you have to use the Rng.Validation.Formula1

See this code.

Sub Sample()
    Dim ws As Worksheet
    Dim dvRng As Range, rng As Range
    Dim strList As String
    Dim MyAr() As String

    Set ws = ThisWorkbook.Sheets("Sheet1")
    Set dvRng = ws.Range("A1") ' Case A
    'Set dvRng = ws.Range("C1") ' Case B
    'Set dvRng = ws.Range("E1") ' Case C

    '~~> Get the formula in the data validation
    strList = dvRng.Validation.Formula1

    '~~> Check if it has an = sign for Case A and Case C
    If InStr(1, strList, "=") Then
        strList = Replace(strList, "=", "")
        Set rng = Range(strList)
        rng.Copy Sheet2.Range("A1")
    Else
    '~~> Case B
        If InStr(1, strList, ",") Then
            MyAr = Split(strList, ",")
            Sheet2.Range("A1:A" & UBound(MyAr) + 1).Resize.Value = Application.Transpose(MyAr)
        Else
            Sheet2.Range("A1").Value = strList
        End If
    End If
End Sub

I have commented the code so you shouldn't face any problems. Still if you do then simply ask :)

like image 180
Siddharth Rout Avatar answered Dec 06 '25 13:12

Siddharth Rout



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!