[英]Excel Data validation list from single cell
我想在一個單元格中創建一個列表以用於數據驗證。
例如:
單格:
| 1-3,6,8 |
數據驗證清單:
1
2
3
6
8
您可以嘗試以下方法:
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
獲取該數據驗證的值,請C1
更改
編寫自定義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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.