单个单元格的Excel数据验证列表

时间:2014-02-14 10:53:40

标签: excel

我想在单个单元格中创建一个用于数据验证的列表。

例如:

单细胞:

| 1-3,6,8 |

数据验证清单:

1

2

3

6

8

2 个答案:

答案 0 :(得分:1)

哟可以试试这个:

Sub DataVal()
    Dim x As String, v, v1, i As Integer, j As Integer, s As String
    x = Range("C1")
    v = Split(x, ",")
    For i = LBound(v) To UBound(v)
       If InStr(v(i), "-") <> 0 Then
          v1 = Split(v(i), "-")
          For j = v1(LBound(v1)) To v1(UBound(v1))
             s = s & j & ","
          Next
       Else
          s = s & v(i) & ","
       End If
    Next
    s = Left(s, Len(s) - 1)
    With Range("D1").Validation
       .Delete
       .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Formula1:=s
    End With
End Sub

我假设您要将数据验证应用于D1单元格,并在C1中获取该数据验证的值,随意更改

答案 1 :(得分:0)

编写自定义VBA功能。非常伪代码如下:

function validateCell(strValues as string, rngValid as range) as boolean
  validateCell = true 'will return true unless the following loop finds an invalid value
  for each value in strValues 'this will require some parsing of strValues parameter
    if value is not in rngValid 
      then validateCells = False
      exit function
    end if
  next value
end function