乔山办公网我们一直在努力
您的位置:乔山办公网 > excel表格制作 > <em>EXCEL</em>中,如何用<em>VBA</em>统计某一区

<em>EXCEL</em>中,如何用<em>VBA</em>统计某一区

作者:乔山办公网日期:

返回目录:excel表格制作


Function MyColorSum(Ref_color As Range, Sum_range As Range)
Application.Volatile
Dim iCol As Integer
Dim rCell As Range
iCol = Ref_color.Interior.ColorIndex
For Each rCell In Sum_range
If iCol = rCell.Interior.ColorIndex Then
MyColorSum = MyColorSum + rCell.Value
End If
Next rCell
End Function
第一个参数是有颜色的单元格,第二个,要统计的区域,马上下班了,只能帮你到这里,希望你能领悟。

sub test()
for each cel in range("A1:D100")
if cel.value="" then
cel="空白"
end if
next
end sub
统计区域某单元格数量,直接用COUNTIF,非要用VBA的话···········...下边这个是个计数函数,用法就是在单元格内直接写=计数(区域)就好,巨具体你要什么代码,自己参照下吧。
Function 计数(rng As Range) As String
Dim arr As Range, i As Byte, arr1, arr2, mystr As String
With CreateObject("scripting.dictionary")
For Each cell In rng
.Item(cell.Value) = .Item(cell.Value) + 1
Next cell
arr1 = .keys
arr2 = .items
For i = 0 To UBound(arr2)
mystr = mystr & arr1(i) & arr2(i) & ","
Next
End With
计数 = Left(mystr, Len(mystr) - 1)
End Function

假设某一区域是A1:D10,填充的内容是"特定内容",如下图:

则VBA代码如下。

Sub MyMacro()
    Dim Rng As Range, c As Range, n As Long
    Set Rng = Range("A1:D10")
    Set c = Rng.SpecialCells(xlCellTypeBlanks)
    n = c.Cells.Count
    c.Value = "特定内容"
    MsgBox "空白单元格的数量是" & n & "个"
End Sub

如下图,运行后会弹出一个对话框说明空白单元格数据,并填充特定内容,结果如下图:

相关阅读

关键词不能为空
极力推荐

ppt怎么做_excel表格制作_office365_word文档_365办公网