vba - Excel VBA : Validating a cell's value after changing it

标签 vba excel excel-2007

我有一个函数,可以在用户将一个单元格更改为某个值后将一系列单元格更新为该值。但是,此范围内的单元格不共享相同的验证范围,因此我想对更新的每个单元格进行单独的验证。

这是我到目前为止所拥有的。对于每个单元格,数据验证设置为其自己的验证范围和可接受的值。由于某种原因,即使数据不在验证范围内,验证仍然成立。我不太确定validation.value属性是如何工作的..

For index = 1 to UBound(someArray)
    wksSomeSheet.Cells(index, column).Value = requiredValue
    If Not wksSomeSheet.Cells(index, column).Validation.Value Then
        MsgBox "A value is not supported for one the cells."
        Exit For
    End If
Next

或者我应该实现它以便使用 Range("validationRange").find(requiredValue) 来代替?

最佳答案

查看了 Siddharth 的博客后,我对他的代码做了一些最小的更改以适应我的需求:

Dim currentValidation As Excel.Validation
wksSomeSheet.Cells(index, Target.Column).Value = requiredValue

Set currentValidation = wksSomeSheet.Cells(index, Target.Column).Validation
If currentValidation.Type = xlValidateList Then
    Dim validationFound As Boolean, MyArray As Variant
    validationFound = False
    MyArray = Application.WorksheetFunction.Transpose(Range(Mid(currentValidation.Formula1, 1)))
    For i = 1 To UBound(MyArray)
        If requiredValue = MyArray(i, 1) Then
            validationFound = True
            Exit For
        End If
    Next

    If Not validationFound Then
        MsgBox wksSomeSheet.Cells(index, wksSomeSheet.Range("myRange").Column).Value & " does not have a valid value."
        Exit For
    End If
End If

关于vba - Excel VBA : Validating a cell's value after changing it,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10179150/

相关文章:

sql - MS Excel 和 SQL Server 中的日期时间转换之间的差异

excel - countif criteria_range 和 criteria 中的名称引用

c# - 无法生成 .xlsx 文件

python - 将制表符分隔值 (TSV) 文件合并到 Excel 2007 (XLSX) 电子表格中

vba - IE 9 不接受 SendKeys

excel - 从 Excel VBA - 检查 Access 表是否存在/如果不存在,则创建/复制

vba - 如何在 PowerPoint VBA 中制作循环?

sql-server - 使用传递参数 Access 调用存储过程

vba - 在 excel 中很难关闭 .exe 进程

javascript - 从表中选择一个单元格并为其指定唯一的类