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

问题描述 投票:0回答:1

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

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

我正在寻找动态的命名范围来解决问题(在其他地方单独提问)。但是我也尝试使用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 =的赋值-该属性是只读的。关于如何解决这个问题的任何建议?

enter image description here

excel vba
1个回答
0
投票

例如:

With Range("A1").Validation

    Debug.Print .Formula1         '>> =LST_VIC

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

    Debug.Print .Formula1         '>> =LST_QLD

End With
© www.soinside.com 2019 - 2024. All rights reserved.