添加数据验证列表,其中条目具有逗号

时间:2015-04-20 11:48:20

标签: excel vba validation

我正在使用此代码将已定义的列表添加到单元格。 我希望填充此列表的条目范围在其名称中包含逗号。然后,代码将此行识别为TempList字符串中的两个或多个项目。有没有办法在项目中包含逗号而不将其分开?

Sub DefinedPartnerListPop()

   Dim rList As String
   Dim MyCol As Collection
   Dim i, n, k, j, a As Integer
   Dim TempList, Partner As String
   Dim WSHT, WSHT2 As Worksheet
   Dim Swap1, Swap2

   Set WSHT = Sheet1

   With WSHT
   a = WorksheetFunction.CountA("A1:A13")
   k = WorksheetFunction.CountA(.Range("A:A")) - a

   Set MyCol = New Collection
   For i = 1 To k
        If Len(Trim(.Range("B" & 13 + i).Value)) <> 0 And .Range("A" & 13 + i) <> "" Then
            On Error Resume Next
          Partner = .Range("B" & 13 + i).Text
            MyCol.Add Partner
            On Error GoTo 0
        End If
    Next i

    For n = 1 To MyCol.Count
        TempList = TempList & "," & MyCol(n)
    Next

    TempList = Mid(TempList, 2)

End With
With WSHT

.Range("B12").ClearContents: .Range("B12").Validation.Delete
With .Range("B12")
 With .Validation
'Call UnProtectChecklistSht
    .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
    xlBetween, Formula1:=TempList
    .IgnoreBlank = True
            .InCellDropdown = True
            .InputTitle = ""
            .ErrorTitle = ""
            .InputMessage = ""
            .ErrorMessage = ""
            .ShowInput = True
            .ShowError = True

End With
End With
End With

End Sub

1 个答案:

答案 0 :(得分:1)

您正尝试将内部 VBA 字符串用作DV条件列表。 VBA 假定逗号分隔DV列表的项目。

如果要在DV列表中包含逗号符号,则应将列表放在数组单元格中并使用该数组单元格作为标准。

请参阅:

My Question