简体   繁体   English

Excel VBA附加验证列表

[英]Excel VBA append a validation list

I have a list validation that I am creating in excel VBA which is based on a set of data from a range. 我有一个在excel VBA中创建的列表验证,该验证基于一个范围内的一组数据。 I then want to add to the list with a set of data from a second range of data. 然后,我想使用来自第二个数据范围的一组数据添加到列表中。 I thought the modify function might work, but all I receive in the list is the data from the first range. 我以为修改功能可能会起作用,但是列表中我收到的只是第一个范围的数据。

I have a list on a worksheet that I am referring to that makes up the range. 我在工作表上有一个列表,该列表构成了范围。 The range has a column for the WCGroupName and a column with unique data. 该范围有一个用于WCGroupName的列和一个包含唯一数据的列。

For example the column on the left is the groups, the column on the right separated by -- is the list: 例如,左侧的列是组,右侧的由-隔开的列是列表:

GROUP -- LIST 组-列表

EXTRUDERS -- 10-EXTRUDER 挤出机-10挤出机
EXTRUDERS -- 15-EXTRUDER 80 挤出机-15挤出机80
MOUNT -- 20-MOUNTER 安装-20安装座
PRESS -- 30-PRESS BONARDI 按-30按BONARDI
BOTTOMSIDE -- 42-BOTTOMSEAL 底部-42底部密封
BOTTOMSIDE -- 72-SIDEWELD BOTTOMPUNCH -- 73-SIDEWELD2 底部-72面底部冲孔-73面2
BOTTOMSEAL -- 40-BOTTOMSEAL 底部-40底部
WICKET -- 60-WICKET 威客-60威客
WICKET -- 62-WICKET 威客-62威客

I want the validation list to show the 4 items in the list. 我希望验证列表显示列表中的4个项目。 The code below only shows the first 2. I am basically trying to append the list so it shows 下面的代码仅显示前2个。我基本上是试图追加列表,以便它显示

42-BOTTOMSEAL, 72-SIDEWELD, 73-SIDEWELD2, 40-BOTTOMSEAL 42底,72侧,73侧2、40底

in a drop down list. 在一个下拉列表中。 This code is nestled inside another loop that is going through all of the WCGROUPNAMEs and creating mulitple validation lists. 此代码嵌套在另一个循环中,该循环遍历所有WCGROUPNAME并创建多个验证列表。 However, I need the validation list assigned to cell B23 on worksheet br to be added, then appended. 但是,我需要添加分配给工作表br上单元格B23的验证列表,然后附加。

Let me know if this is even possible. 让我知道这是否有可能。

I have modified my code insert to show a broader scope of what I'm doing. 我已经修改了代码插入,以显示我正在做的事情的广泛范围。 The GetUniqueWCGroup is a function that creates a piped list. GetUniqueWCGroup是创建管道列表的函数。

'This section will create the list lookups for each WC type
WCGroup = GetUniqueWCGroup()
WCGroupArray = Split(WCGroup, "|")

'Create a report for each team
For Each tmp In WCGroupArray

    WCGroupName = CStr(tmp)
    wg = WCGroupName
    'identify last row for the group
    wc.Activate
    frwg = lrwg + 1
    lrwg = xlLastRowWCGroup(wg)

    If WCGroupName = "MOUNT" Then

        Set l = br.Range("B21:B21")
        Set R = wc.Range("B" & frwg & ":B" & lrwg)

        With l.Validation
            .Delete 'delete previous validation
            .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
                Formula1:="='" & wc.Name & "'!" & R.Address
        End With

    End If 'WCGroup Mounter

    If WCGroupName = "PRESS" Then

        Set l = br.Range("B22:B22")
        Set R = wc.Range("B" & frwg & ":B" & lrwg)

        With l.Validation
            .Delete 'delete previous validation
            .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
                Formula1:="='" & wc.Name & "'!" & R.Address
        End With
    End If 'WCGroup Press

    If WCGroupName = "BOTTOMSEAL" Or WCGroupName = "BOTTOMSIDE" Then

        If bc = 1 Then

            Set l = br.Range("B23:B23")
            Set R = wc.Range("B" & frwg & ":B" & lrwg)

            With l.Validation
                .Delete 'delete previous validation
                .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
                    Formula1:="='" & wc.Name & "'!" & R.Address
            End With

            bc = bc + 1
        Else

            Set l = br.Range("B23:B23")
            Set R = wc.Range("B" & frwg & ":B" & lrwg)

            With l.Validation
                '.Delete 'delete previous validation
                .Modify Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
                    Operator:=xlBetween, Formula1:="='" & wc.Name & "'!" & R.Address
            End With

        End If


    End If 'WCGroup BottomSeal or BottomSide

    If WCGroupName = "WICKET" Then

        Set l = br.Range("B24:B24")
        Set R = wc.Range("B" & frwg & ":B" & lrwg)

        With l.Validation
            .Delete 'delete previous validation
            .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
                Formula1:="='" & wc.Name & "'!" & R.Address
        End With
    End If 'WCGroup Wicket

    If WCGroupName = "BOTTOMSIDE" Then

        Set l = br.Range("B25:B25")
        Set R = wc.Range("B" & frwg & ":B" & lrwg)

        With l.Validation
            .Delete 'delete previous validation
            .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
                Formula1:="='" & wc.Name & "'!" & R.Address
        End With
    End If 'WCGroup BottomSide

    If WCGroupName = "SLITTER" Then

        Set l = br.Range("B26:B26")
        Set R = wc.Range("B" & frwg & ":B" & lrwg)

        With l.Validation
            .Delete 'delete previous validation
            .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
                Formula1:="='" & wc.Name & "'!" & R.Address
        End With
    End If 'WCGroup Slitting

    If WCGroupName = "PERFORATOR" Then

        Set l = br.Range("B27:B27")
        Set R = wc.Range("B" & frwg & ":B" & lrwg)

        With l.Validation
            .Delete 'delete previous validation
            .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
                Formula1:="='" & wc.Name & "'!" & R.Address
        End With

    End If 'Perforator

    If WCGroupName = "OP" Then

        Set l = br.Range("B29:B29")
        Set R = wc.Range("B" & frwg & ":B" & lrwg)

        With l.Validation
            .Delete 'delete previous validation
            .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
                Formula1:="='" & wc.Name & "'!" & R.Address
        End With

        Set l = br.Range("B30:B30")
        Set R = wc.Range("B" & frwg & ":B" & lrwg)

        With l.Validation
            .Delete 'delete previous validation
            .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
                Formula1:="='" & wc.Name & "'!" & R.Address
        End With

        Set l = br.Range("B31:B31")
        Set R = wc.Range("B" & frwg & ":B" & lrwg)

        With l.Validation
            .Delete 'delete previous validation
            .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
                Formula1:="='" & wc.Name & "'!" & R.Address
        End With

        Set l = br.Range("B32:B32")
        Set R = wc.Range("B" & frwg & ":B" & lrwg)

        With l.Validation
            .Delete 'delete previous validation
            .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
                Formula1:="='" & wc.Name & "'!" & R.Address
        End With

    End If 'OP

Next

As you may know, there are two ways to create a DV list: 您可能知道,有两种创建DV列表的方法:

  1. worksheet cells 工作表单元格
  2. comma separated string (css) 逗号分隔的字符串(css)

the second way is like: 第二种方式是:

.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
        xlBetween, Formula1:="larry,moe,curley"

This means that if you have two or more disjoint ranges, you can create a DV list by making a single css 这意味着,如果您有两个或多个不相交的范围,则可以通过创建一个css来创建DV列表

In this example we combine the contents of A1:A5 and A8:A10 into a single DV: 在此示例中,我们将A1:A5A8:A10的内容合并为一个DV:

Sub DisJointDatJoint()
    Dim rDV As Range, sDV As String
    Set rDV = Range("A1:A5,A8:A10")
    sDV = ""
    For Each r In rDV
        sDV = sDV & r.Value & ","
    Next r
    sDV = Mid(sDV, 1, Len(sDV) - 1)
    With Selection.Validation
            .Delete
            .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
            xlBetween, Formula1:=sDV
            .IgnoreBlank = True
            .InCellDropdown = True
            .InputTitle = ""
            .ErrorTitle = ""
            .InputMessage = ""
            .ErrorMessage = ""
            .ShowInput = True
            .ShowError = True
    End With
End Sub

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM