excel - 使用 VBA 计算范围内彩色单元格的数量
问题描述
我有一个计算彩色单元格数量的代码。你能建议如何扩大范围吗?基本上我需要计算彩色单元格列范围,如(“A:Z”)和 nRowIndex = 2 到 100?
Sub CountCellsWithBackgroundColor()
Dim nRowIndex As Integer, nCellNumber As Integer
'Go through the range
Worksheets("Report_Rule_S").Activate
For nRowIndex = 2 To 100
If Range("E" & nRowIndex).Interior.ColorIndex <> -4142 Then 'need to do somthing like Range("A:Z")
nCellNumber = nCellNumber + 1
End If
Next nRowIndex
' Output the result
Worksheets("cover").Range("B12") = nCellNumber
End Sub
解决方案
您可以将所有这些放在一个 For-Each 循环中:
Dim aCell As Range
Dim cellNumber As Integer
cellNumber = 0
For Each aCell In Range("A2:Z100").Cells:
If aCell.Interior.ColorIndex <> -4142 Then
cellNumber = cellNumber + 1
End If
Next
推荐阅读
- kotlin - PeriodicWorkRequest 不重复
- python - PySpark where 子句条件条件
- c# - 在 IIS 上的已发布应用程序中,每次我重新启动服务器或更改应用程序配置中的某些内容时,都会卸载项目引用
- java - 需要澄清 Oracle 教程解释何时使用迭代器与 for-each 构造
- android - android:停止通知振动
- angular - 角度可选 mat-optgroup
- python-3.x - 如何解决 webrtcvad.Error:处理帧时出错?
- java - java.lang.OutOfMemoryError:无法在 Windows 中创建新的本地线程 ..recreate
- c - 这个 c 程序 null 安全吗?
- python - 熊猫多索引,删除条件仍然成立的行?