excel 用VBA快速计数有色细胞

yftpprvb  于 2023-06-30  发布在  其他
关注(0)|答案(1)|浏览(195)

我有一个电子表格选项卡,其中的活动范围是A4:DD 2500,因此有270,000个单元格。我有一段代码(感谢@EvilBlueMonkey),它根据用户在A列中的下拉选择(见下文),将B列到DD列中的单元格填充为蓝色、灰色或黄色。当用户在其中一个蓝色单元格中输入数据时,它会变成绿色,表示使用条件格式完成。我想实现一个代码,在每次用户执行一个条目后动态运行,该条目计数四种不同的情况-蓝色单元格,绿色单元格,没有文本的黄色单元格和有文本的黄色单元格。我构建了以下代码,它将运行,但每次都会冻结Excel。有没有什么方法可以更有效地遍历27万个单元格?如果没有,那么循环遍历那些只填充了列A的行怎么办?

  1. Option Explicit
  2. Private Sub Worksheet_SelectionChange(ByVal Target As Range)
  3. 'Declarations.
  4. Dim CountRange As Range
  5. Dim CountRangeCell As Range
  6. Dim BColorCounter As Long
  7. Dim GColorCounter As Long
  8. Dim YColorCounter As Long
  9. Dim YColorTextCounter As Long
  10. 'SETTINGS:
  11. 'Set Cells to be counted Range
  12. Set CountRange = Worksheets("ADIS").Range("B3:DD2500")
  13. 'Loop through each cell in the range
  14. For Each CountRangeCell In CountRange
  15. 'Checking Blue Color
  16. If Cells(CountRangeCell.Row, CountRangeCell.Column).DisplayFormat.Interior.Color = RGB(155, 194, 230) Then
  17. BColorCounter = BColorCounter + 1
  18. Else
  19. 'Checking Yellow Color
  20. If Cells(CountRangeCell.Row, CountRangeCell.Column).DisplayFormat.Interior.Color = RGB(255, 255, 0) And CountRangeCell.Text = "" Then
  21. YColorCounter = YColorCounter + 1
  22. Else
  23. 'Checking Green Color
  24. If Cells(CountRangeCell.Row, CountRangeCell.Column).DisplayFormat.Interior.Color = RGB(169, 208, 142) Then
  25. GColorCounter = GColorCounter + 1
  26. Else
  27. 'Checking Yellow With Text
  28. If Cells(CountRangeCell.Row, CountRangeCell.Column).DisplayFormat.Interior.Color = RGB(255, 255, 0) And CountRangeCell.Value <> "" Then
  29. YColorTextCounter = YColorTextCounter + 1
  30. End If
  31. End If
  32. End If
  33. End If
  34. Next
  35. Range("C2504") = YColorCounter
  36. Range("D2504") = BColorCounter
  37. Range("E2504") = GColorCounter
  38. Range("F2504") = YColorTextCounter
  39. End Sub

谢谢!x1c 0d1x

watbbzwu

watbbzwu1#

工作表变更:计数突出显示的单元格

  • 请注意,这是一个很大的范围,如果删除条件 “如果范围第一列的值为空,则不应计算该行”,代码将花费大约5秒(在A列中的每个更改上)。如果列A中的所有单元格都被填充。
  • 我认为这一切都是XY problem。要走的路将是实现逻辑,你已经使用了条件格式摆在首位,到VBA。但这需要更多的信息。
  1. Option Explicit
  2. ' The worksheet name ("ADIS") is irrelevant. Use the 'Me' keyword instead.
  3. ' Whenever you change a value in the 1st column of the range,
  4. ' this code runs automatically counting the colors in the remaining columns.
  5. ' It is assumed that blank cells in the first column will have no highlights
  6. ' in the remaining columns.
  7. Private Sub Worksheet_Change(ByVal Target As Range)
  8. Const SHOW_MESSAGE_BOX As Boolean = True
  9. Dim Success As Boolean
  10. On Error GoTo ClearError
  11. Const SOURCE_RANGE As String = "A3:DD2500"
  12. Dim Blue As Long: Blue = RGB(155, 194, 230)
  13. Dim Green As Long: Green = RGB(169, 208, 142)
  14. Dim Yellow As Long: Yellow = RGB(255, 255, 0)
  15. Dim trg As Range, CountRange As Range
  16. With Me.Range(SOURCE_RANGE)
  17. Set trg = .Columns(1)
  18. ' If a change didn't happen in the first column, do nothing.
  19. If Intersect(trg, Target) Is Nothing Then Exit Sub
  20. Set CountRange = .Resize(, .Columns.Count - 1).Offset(, 1)
  21. End With
  22. Dim CountRangeRow As Range, CountRangeCell As Range, r As Long
  23. Dim BlueCount As Long, GreenCount As Long
  24. Dim YellowCount As Long, YellowTextCount As Long
  25. For Each CountRangeRow In CountRange.Rows
  26. r = r + 1
  27. If Len(trg.Cells(r)) > 0 Then
  28. For Each CountRangeCell In CountRangeRow.Cells
  29. Select Case CountRangeCell.DisplayFormat.Interior.Color
  30. Case Blue
  31. BlueCount = BlueCount + 1
  32. Case Green
  33. GreenCount = GreenCount + 1
  34. Case Yellow
  35. If Len(CStr(CountRangeCell.Value)) = 0 Then ' is blank
  36. YellowCount = YellowCount + 1
  37. Else ' is not blank
  38. YellowTextCount = YellowTextCount + 1
  39. End If
  40. 'Case Else ' neither; do nothing
  41. End Select
  42. Next CountRangeCell
  43. End If
  44. Next CountRangeRow
  45. ' Before writing to the worksheet, disable events or the code
  46. ' gets triggered again and again until Excel crashes!
  47. Application.EnableEvents = False
  48. With Me
  49. .Range("C2504").Value = YellowCount
  50. .Range("D2504").Value = BlueCount
  51. .Range("E2504").Value = GreenCount
  52. .Range("F2504").Value = YellowTextCount
  53. End With
  54. Success = True
  55. ProcExit:
  56. On Error Resume Next
  57. ' Don't forget to enable events again!
  58. If Not Application.EnableEvents Then Application.EnableEvents = True
  59. If SHOW_MESSAGE_BOX Then
  60. If Success Then MsgBox "Highlighted cells counted.", vbInformation
  61. End If
  62. On Error GoTo 0
  63. Exit Sub
  64. ClearError:
  65. MsgBox "Run-time error '" & Err.Number & "':" & vbLf & vbLf _
  66. & Err.Description, vbCritical
  67. Resume ProcExit
  68. End Sub
展开查看全部

相关问题