簡體   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