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

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

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

  1. Sub updateDataValidationRecords()
  2. Range("A7").Validation.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
  3. Formula1:="=Status"
  4. End Sub

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

z3yyvxxp

z3yyvxxp1#

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

  1. Sub updateDataValidationRecordsBasedOnCell(Optional cellAddress As String)
  2. Dim ws As Worksheet
  3. Dim validationRange As Range
  4. Dim rowDataRange As Range
  5. ' Set the worksheet where you want to apply the data validation
  6. Set ws = ThisWorkbook.Sheets("YourSheetName") ' Replace "YourSheetName" with the actual sheet name
  7. ' If cellAddress is not provided, ask the user for input
  8. If cellAddress = "" Then
  9. On Error Resume Next
  10. cellAddress = InputBox("Enter cell address:", "Cell Address", ActiveCell.Address)
  11. On Error GoTo 0
  12. End If
  13. ' Exit the sub if the user cancels the InputBox
  14. If cellAddress = "" Then Exit Sub
  15. ' Set the validation range based on the provided cell address
  16. Set validationRange = ws.Range(cellAddress)
  17. ' Clear existing data validation in the specified range
  18. validationRange.Validation.Delete
  19. ' Determine the entire row for data validation starting from column A
  20. Set rowDataRange = ws.Rows(validationRange.Row)
  21. ' Add data validation to the entire row based on the named range "Status"
  22. With rowDataRange.Validation
  23. .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Formula1:="=Status"
  24. .IgnoreBlank = True
  25. .InCellDropdown = True
  26. ' You can customize other validation properties here if needed
  27. End With

字符串
End Sub

展开查看全部
31moq8wy

31moq8wy2#

可能对你的案子有用

  1. Sub updateDataValidationRecordsBasedOnCell(cellAddress As String)
  2. Dim ws As Worksheet
  3. Dim validationRange As Range
  4. Dim rowDataRange As Range
  5. ' Set the worksheet where you want to apply the data validation
  6. Set ws = ThisWorkbook.Sheets("YourSheetName") ' Replace "YourSheetName" with the actual sheet name
  7. ' Set the validation range based on the provided cell address
  8. Set validationRange = ws.Range(cellAddress)
  9. ' Clear existing data validation in the specified range
  10. validationRange.Validation.Delete
  11. ' Determine the entire row for data validation starting from column A
  12. Set rowDataRange = ws.Rows(validationRange.Row)
  13. ' Add data validation to the entire row based on the named range "Status"
  14. With rowDataRange.Validation
  15. .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Formula1:="=Status"
  16. .IgnoreBlank = True
  17. .InCellDropdown = True
  18. ' You can customize other validation properties here if needed
  19. End With
  20. End Sub
  21. Sub TestUpdateDataValidation()
  22. updateDataValidationRecordsBasedOnCell "A7"
  23. ' Change the cell address as needed
  24. End Sub

字符串

展开查看全部

相关问题