使用excel vba将字体颜色的总和添加到每列的末尾

时间:2014-05-14 15:00:20

标签: vba excel-vba excel

需要按字体颜色加一些列。我只有上面的代码当然可以总结一切。

Sub sum()
With Sheets("sheet4").Range("a1").CurrentRegion
    If .Cells(.Rows.Count, 1).Value <> "Total" Then
        With .Offset(.Rows.Count).Resize(1)
            .Formula = "=sum(r2c:r[-1]c)"
            .Columns(1).Value = "Total"
        End With
    End If
End With
End Sub

1 个答案:

答案 0 :(得分:2)

如果您需要遍历某个范围并按字体颜色执行某些操作,则可以执行以下操作。要使用它,您需要传递它想要查看的范围,以及一个具有您想要在其中查找的字体颜色的范围。这比输入要查找的绿色阴影的全色数更容易 - 只需将绿色的单元格复制到另一个图纸,然后将其作为参数传递。

Sub DoSomethingByFontColor(ByRef xlRange As Excel.Range, ByRef checkColorrange As Excel.Range)

Dim xlcell As Excel.Range, i as long

For Each xlcell In xlRange
    If xlcell.Font.Color = checkColorrange.Font.Color Then
        'do something
        i = i + 1
    End If
Next xlcell

Debug.Print "There were " & i & "incidences of font color " & _
checkColorrange.Font.Color & " in the check range."

End Sub

Sub test()

DoSomethingByFontColor Sheet1.Range("C1:C5"), Sheet1.Range("a1")

End Sub