通过 VBA 更新损坏的数据验证列表

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

我有一个包含多个验证列表的电子表格,其中一个链接到条件格式以识别记录的状态,“已关闭”,“打开”,“待处理”等。 sptreadsheet 相当大,并且具有删除和插入功能随着时间的推移,某些行的条件格式状态数据验证列表已损坏。

我的手动清理过程非常耗时,我正在尝试通过 vba 以编程方式重新创建损坏的行。此代码片段有效,但这是硬编码的,我每次都必须手动更改每个范围引用。

Sub updateDataValidationRecords()

Range("A7").Validation.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
Formula1:="=Status"

End Sub

问题:

有没有办法对上面的代码进行变体,我可以提供单个单元格地址,并根据该单元格地址,为同一行中需要数据验证列表的其他单元格添加相应的数据验证列表?

提前感谢您的帮助,非常感谢。

excel vba object range
1个回答
0
投票

可能适合您的情况:

Sub updateDataValidationRecordsBasedOnCell(cellAddress As String) 昏暗的工作表 昏暗的验证范围作为范围 昏暗的 rowDataRange 作为范围

' Set the worksheet where you want to apply the data validation
Set ws = ThisWorkbook.Sheets("YourSheetName") ' Replace "YourSheetName" with the actual sheet name

' Set the validation range based on the provided cell address
Set validationRange = ws.Range(cellAddress)

' Clear existing data validation in the specified range
validationRange.Validation.Delete

' Determine the entire row for data validation starting from column A
Set rowDataRange = ws.Rows(validationRange.Row)

' Add data validation to the entire row based on the named range "Status"
With rowDataRange.Validation
    .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Formula1:="=Status"
    .IgnoreBlank = True
    .InCellDropdown = True
    ' You can customize other validation properties here if needed
End With

结束子

通过此子尝试上面的代码:

子测试更新数据验证() 更新数据验证记录基于单元格“A7” ' 根据需要更改单元格地址 结束子

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