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

mmvthczy  于 2023-11-20  发布在  其他
关注(0)|答案(2)|浏览(87)

我有一个电子表格与几个验证列表,其中一个是链接到条件格式,以确定记录的状态,“关闭”,“打开”,“挂起”等电子表格是相当大的,随着时间的推移删除和插入行的条件格式化状态数据验证列表的一些行是坏的。
我的手动清理过程很耗时,我试图通过vba编程重新创建损坏的行。这段代码可以工作,但这是硬编码,我必须每次手动更改每个范围引用。

Sub updateDataValidationRecords()

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

End Sub

字符串
问:
有没有一种方法可以让上面的代码发生变化,我可以提供一个单元格地址,并基于该单元格地址,为同一行中需要数据验证列表的其他单元格添加相应的数据验证列表?
提前感谢你的帮助,非常感谢。

z3yyvxxp

z3yyvxxp1#

不知道我是否理解了你的问题,但这可能会像你描述的那样工作。

Sub updateDataValidationRecordsBasedOnCell(Optional cellAddress As String)
Dim ws As Worksheet
Dim validationRange As Range
Dim rowDataRange As Range

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

' If cellAddress is not provided, ask the user for input
If cellAddress = "" Then
    On Error Resume Next
    cellAddress = InputBox("Enter cell address:", "Cell Address", ActiveCell.Address)
    On Error GoTo 0
End If

' Exit the sub if the user cancels the InputBox
If cellAddress = "" Then Exit Sub

' 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

字符串
End Sub

31moq8wy

31moq8wy2#

可能对你的案子有用

Sub updateDataValidationRecordsBasedOnCell(cellAddress As String)
    Dim ws As Worksheet
    Dim validationRange As Range
    Dim rowDataRange As Range
    
    ' 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
End Sub


Sub TestUpdateDataValidation()
    updateDataValidationRecordsBasedOnCell "A7"
    ' Change the cell address as needed
End Sub

字符串

相关问题