简体   繁体   English

Excel 或 VBA - 从另一个工作表中的动态列填充下拉列表,没有重复项

[英]Excel or VBA - populate a drop down list from a dynamic column in another sheet with no duplicates

I don't know if this is even possible, but I am trying to do a couple of things that require me to populate drop down lists, and a normal cell by cell list, based on the items in a column on another sheet.我不知道这是否可能,但我正在尝试做一些事情,这些事情需要我根据另一张工作表上一列中的项目填充下拉列表和一个普通的单元格列表。 I know how to do this with a regular column, but the column I want to use changes length and has many duplicate values in it, as it is an inventory of sorts.我知道如何使用常规列执行此操作,但是我要使用的列更改了长度并且其中包含许多重复值,因为它是各种清单。

So on this sheet, in C5,所以在这张纸上,在 C5 中,

在此处输入图片说明 that value should be able to be selected from a drop down list based on the contents of column B in the second sheet, but also without having to scroll through duplicates.该值应该能够根据第二个工作表中 B 列的内容从下拉列表中选择,而且无需滚动重复项。 在此处输入图片说明

I am happy to use either VBA or general excel formulas to achieve this.我很高兴使用 VBA 或通用 excel 公式来实现这一点。 I am also aware that I don't think I have explained this very well so please feel free to prompt me for more information as required.我也知道我认为我没有很好地解释这一点,所以请随时根据需要提示我提供更多信息。

Here is a start.这是一个开始。 I assume the the list of items is in Sheet2 somewhere from B1 through B1000 .我假设项目列表位于从B1B1000 的Sheet2 中 It's OK if the range is only partially filled (adjust the 1000 to suit your needs) .如果范围只是部分填充(调整 1000 以满足您的需要),那也没关系。

The code scans this list and builds a DV string.代码扫描此列表并构建 DV 字符串。 The Data Validation is then applied to Sheet1 cell C5 :然后将数据验证应用于Sheet1单元格C5

Sub setupDV()
    Dim rSource As Range, rDV As Range, r  As Range, csString As String
    Dim c As Collection

    Set rSource = Sheets("Sheet2").Range("B1:B1000")
    Set rDV = Sheets("Sheet1").Range("C5")
    Set c = New Collection
    csString = ""
    On Error Resume Next
    For Each r In rSource
        v = r.Value
        If v <> "" Then
            c.Add v, CStr(v)
            If Err.Number = 0 Then
                If csString = "" Then
                    csString = v
                Else
                    csString = csString & "," & v
                End If
            Else
                Err.Number = 0
            End If
        End If
    Next r
    On Error GoTo 0

    'MsgBox csString

    With rDV.Validation
        .Delete
        .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
        xlBetween, Formula1:=csString
        .IgnoreBlank = True
        .InCellDropdown = True
        .InputTitle = ""
        .ErrorTitle = ""
        .InputMessage = ""
        .ErrorMessage = ""
        .ShowInput = True
        .ShowError = False
    End With
End Sub

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Excel VBA 从工作表填充父子下拉源 - Excel VBA to populate parent child drop down source from sheet 通过下拉列表将数据从一张纸导出到另一张纸-VBA Excel Macro - Export data from one sheet to another via drop down list - VBA Excel Macro Excel VBA - 根据 3 个条件(复杂的 IF AND 相关 VBA 与通配符)使用另一张工作表中的值填充一张工作表上的列 - Excel VBA - Populate a column on one sheet with values from another sheet based on 3 criteria (complicated IF AND related VBA with wildcards) 如何使用VBA在Excel中读取工作表上的下拉列表的值 - How to read the value of a drop down list on a sheet in Excel using VBA 使用单元格下拉列表中的值填充 vba ComboBox - Populate a vba ComboBox with the values from the drop-down list of a cell 将 Excel VBA 代码集成到 Java 代码中,最终创建从 Excel 表格单元格中的下拉列表中选择多个值的功能 - Integrate Excel VBA code to Java code to finally create the feature of selecting multiple values from the drop down list in a cell of an excel sheet 从工作表B填充工作表A-Excel VBA - populate sheet A from sheet B - excel VBA 从列中删除重复项,但使用VBA使另一张表上的列成为结果 - Removing Duplicates from a Column but make columns on another sheet the outcome with VBA 如何通过 VBA 代码填充下拉列表? - How to populate a drop down list by VBA code? Excel VBA脚本有条件地填充另一个工作表 - Excel VBA script to populate another sheet conditionally
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM