Edit or export drop down lists in Excel

前端 未结 2 1818
面向向阳花
面向向阳花 2020-12-21 13:54

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

相关标签:
2条回答
  • 2020-12-21 14:13

    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 :)

    0 讨论(0)
  • 2020-12-21 14:16

    Unless someone has a better idea, I've selected each of the items into a different cell, then copied that into a csv file where I can import into a table.

    0 讨论(0)
提交回复
热议问题