excel在选定区域中进行数量统计的VBA代码
时间:2022-03-30 23:47:39
如果在Excel中选择了一个或多个区域,用下面的VBA代码可以统计当前工作表所选区域中单元格(或行、列)的数量:
1. 统计选定区域中的单元格数量:
Sub CountCellsInSelection()
Dim CellsNum As Integer
CellsNum = Selection.Count
MsgBox "所选区域中的单元格数量为: " & CellsNum
End Sub
2.统计选定区域中所包含的行数,如果选择了多个区域,则统计行数之和。
Sub CountRowsInSelection()
Dim RowsNum As Integer
For i = 1 To Selection.Areas.Count
RowsNum = RowsNum + Selection.Areas(i).Rows.Count
Next i
MsgBox "所选区域中的行数为: " & RowsNum
End Sub
3.统计选定区域中所包含的列数,如果选择了多个区域,则统计列数之和。
Sub CountColumnsInSelection()
Dim ColumnsNum As Integer
For i = 1 To Selection.Areas.Count
ColumnsNum = ColumnsNum + Selection.Areas(i).Columns.Count
Next i
MsgBox "所选区域中的列数为: " & ColumnsNum
End Sub
4.统计选定区域中的非空单元格数量:
Sub CountNonBlankInSelection()
Dim NonBlankNum As Integer
NonBlankNum = Application.CountA(Selection)
MsgBox "所选区域中包含非空单元格有" & NonBlankNum & "个。"
End Sub
5.统计选定区域中有填充色的单元格数量:
Sub CountColorCellsInSelection()
Dim ColorCellsNum As Integer
Dim rCell As Range
For Each rCell In Selection
If rCell.Interior.ColorIndex > 0 Then
ColorCellsNum = ColorCellsNum + 1
End If
Next rCell
MsgBox "所选区域中填充了颜色的单元格有" & ColorCellsNum & "个。"
End Sub
6.统计选定区域中包含公式的单元格数量:
Sub CountFormulaInSelection()
Dim FormulaNum As Integer
Dim rCell As Range
For Each rCell In Selection
If rCell.HasFormula Or rCell.HasArray Then
FormulaNum = FormulaNum + 1
End If
Next rCell
MsgBox "所选区域中包含公式的单元格有" & FormulaNum & "个。"
End Sub
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
excel批注怎么设置
excel图表制作:创建级联列表框
![](https://img.aspxhome.com/file/2023/2/a142632_0s.gif)
Excel中表格进行设置非打印区域成灰色的操作方法
Excel如何设置减少加载项?Excel设置减少加载项教程
![](https://img.aspxhome.com/file/2023/0/39820_0s.jpg)
Excel表格也能做成EXE可执行文件
怎么样把wordWPS版本表格里面的回车符号隐藏掉?
在Word中怎么打平方米符号
如何在Word中输入拼音声调
![](https://img.aspxhome.com/file/2023/8/19848_0s.png)
excel表格删除数据行的教程
在Word中使用项目符号有何诀窍
![](https://img.aspxhome.com/file/2023/3/24783_0s.png)
word如何使用选择性粘贴
在Word中带圈字符怎么创建
![](https://img.aspxhome.com/file/2023/1/18371_0s.jpg)
如何对Excel工作表中数据随机排序?
![](https://img.aspxhome.com/file/2023/8/37258_0s.png)
如何冻结excel表格行和列?(图文)
![](https://img.aspxhome.com/file/2023/7/43067_0s.jpg)
如何在excel中输入身份证号码
![](https://img.aspxhome.com/file/2023/8/a154748_0s.png)
word 单张纸横向怎么设置
![](https://img.aspxhome.com/file/2023/2/32622_0s.png)
如何在Word 2013中过滤合并数据源?
![](https://img.aspxhome.com/file/2023/1/23261_0s.jpg)
Win10专业版重启后都还原了怎么解决?Win10重启后都还原了解决方法
![](https://img.aspxhome.com/file/2023/3/49483_0s.jpg)
win10键盘部分按键失灵怎么办
![](https://img.aspxhome.com/file/2023/4/50854_0s.jpg)