简体   繁体   中英

Excel Data validation list from single cell

I would like to create in a single cell a list to use for data validation.

For example:

Single cell:

| 1-3,6,8 |

Data validation list:

1

2

3

6

8

Yo can try this:

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

I assume you want to apply data validation to D1 cell and get the values for that data validation in C1 , change at will

Write a custom VBA function. Very pseudo-code follows:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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