如何通过数据验证在下拉列表中添加更多约500条记录?

时间:2019-01-29 09:44:33

标签: excel excel-formula

我正在尝试制作一个Excel工作表作为示例模板,以供用户上传批量数据。我想在数据验证的列中将列表显示为下拉列表。由于服务器端的验证,我无法使用第二张表或第二列作为参考。当我在Google工作表的下拉列表中添加逗号分隔的记录并下载并打开它时,我无法打开该下拉列表,我猜这是因为限制或某种原因。请帮我在这里。

1 个答案:

答案 0 :(得分:0)

如果您想使用VBA,可以尝试:

Option Explicit

Sub test()

    Dim LastRow As Long, Row As Long, strList As String
    Dim rng As Range

    With ThisWorkbook.Worksheets("Sheet1")

        'Set rng as cell B1
        Set rng = .Range("B1")

        'Find last row fo column A
        LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row

        strList = ""

        'Loop Column A, starting from row 1 and ends at Last row column A
        For Row = 1 To LastRow

            If strList = "" Then
                'If strList is "" then takes the value of .Cells(Row, 1)
                strList = .Cells(Row, 1).Value
            Else
                'If strList not "" takes the value of strList + "," + .Cells(Row, 1)
                 strList = strList & "," & .Cells(Row, 1).Value
            End If

        Next Row

        'Set drop down list on cell B1
        With rng.Validation
            .Delete 'Delete previous validation list
            .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
                Formula1:=strList 'Import new validation list
        End With

    End With

End Sub