閱: 12776 | 回: 18
等級:學(xué)者-
積分:99 -
財(cái)富值:250 -
身份:普通用戶
按顏色求和的, 已經(jīng)搞定了
VBA代碼如下, 記得要配合方方格子的收納箱使用。很簡單的
Option Explicit
Sub 按顏色求和()
On Error Resume Next
Dim sRng As Range
Dim cRng As Range
Dim result As Range
Dim r As Range
Dim choice As Integer
Set sRng = Selection
Dim ncolor As Long
Dim total As Double
choice = Application.InputBox("選擇統(tǒng)計(jì)方式,0 為按背景顏色統(tǒng)計(jì), 1為按字體顏色統(tǒng)計(jì)", "統(tǒng)計(jì)方式", Default:=0, Type:=1)
If choice = 0 Or choice = 1 Then
Else
MsgBox "無效選項(xiàng),必須為0 或者 1"
Exit Sub
End If
Set cRng = Application.InputBox("選擇需要統(tǒng)計(jì)的顏色的一個(gè)單元格(只需一個(gè)單元格)", "選擇單元格", Type:=8)
If cRng Is Nothing Then Exit Sub
Set cRng = cRng.Cells(1, 1)
If choice = 0 Then '背景
ncolor = cRng.Interior.Color
Else
ncolor = cRng.Font.Color
End If
total = 0
For Each r In sRng
If IsNumeric(r.Value) Then
If choice = 0 Then '背景
If r.Interior.Color = ncolor Then
total = total + CDbl(r.Value)
End If
Else
If r.Font.Color = ncolor Then
total = total + CDbl(r.Value)
End If
End If
End If
Next
Set result = Application.InputBox("選擇結(jié)果存放位置", "選擇單元格", Type:=8)
If result Is Nothing Then Exit Sub
result.Value = total
End Sub
我只是一個(gè)打工仔..
等級:初學(xué)者
等級:傳說級人物