繁体   English   中英

更改 VBA 中的单元格验证列表源

[英]Changing Cell Validation List source in VBA

我在 CALC 表中有一系列单元格,这些单元格使用另一个 DATA_VIC 表中的命名范围进行验证。 计算表是通用的 - 但它提取的数据是地理(澳大利亚州)特定的(VIC - 维多利亚)。 本数据表中的所有命名范围均以 state“VIC”结尾。

现在我必须修改 CALC 表以便能够使用来自两种不同状态的数据,具体取决于在 CALC 表中的特定单元格 (selSTATE) 中选择了哪个 state(VIC 或 ALD)。

我正在寻找动态命名范围来解决问题(其他地方的单独问题)。 但我也尝试使用 VBA 更改将受更改影响的单元格上的验证列表。 代码看起来像这样:

Sub ChangeValidation(strState As String)
Dim rng As Range ' rng object for looping

' Loop through the range of all user entry cells in the sheet
For Each rng In Range("AllUserEntryCells")

    ' Check to see if this cell is a validation list
    If rng.Validation.Type = xlValidateList Then

        ' Check to see if the validation list is State-Specific (Ends in either QLD or VIC)
        If InStr("QLDVIC", Right(rng.Validation.Formula1, 3)) <> 0 Then

            ' Check to see if the state is not set to the current state
            If Right(rng.Validation.Formula1, 3) <> strState Then

                ' Set the validation list to the new state list
                rng.Validation.Formula1 = Left(rng.Validation.Formula1, Len(rng.Validation.Formula1) - 3) & strState

            End If
        End If
    End If
Next
End Sub

问题是.Validation.Formula1 =当然的分配 - 该属性是只读的。 关于如何解决这个问题的任何建议?

在此处输入图像描述

例如:

With Range("A1").Validation

    Debug.Print .Formula1         '>> =LST_VIC

    .Modify Formula1:=Replace(.Formula1, "VIC", "QLD")

    Debug.Print .Formula1         '>> =LST_QLD

End With

https://docs.microsoft.com/en-us/office/vba/api/excel.validation.modify

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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