包含特定字体颜色的单元格总数

时间:2019-04-14 14:06:58

标签: excel vba excel-formula

我正在尝试创建一个程序,以汇总工作簿中所有工作表中A列中包含绿色和红色字体的单元格的数量。

在代码下方的提供的代码中,它对工作表的 A列中包含绿色和红色字体的单元格进行全部计数。

如果您可以引导我正确的方向,请务必发表评论!

我还制作了一个示例Google表格来说明我要完成的工作:https://docs.google.com/spreadsheets/d/1yLfCxaT-cIl_W77Y67xdg_ZTSQlg9X2a5vxAH4JtDpk/edit?usp=sharing

' If it's not going to return something, you can define this as a procedure (sub) and not a function
Sub Test_It()

    Dim mySheet As Worksheet ' Define as worksheet if you're going to loop through sheets and none is a Graph/Chart sheet

    Dim printRow As Integer ' Beware that integer it's limited to 32k rows (if you need more, use Long)
    printRow = 2


    For Each mySheet In ThisWorkbook.Sheets ' use the mySheet object previously defined

        Range("N" & printRow).Value = "Sheet Name:"
        Range("O" & printRow).Value = mySheet.Name
        Range("P" & printRow).Value = "Approval:"
        Range("Q" & printRow).Value = SumGreen(mySheet) ' you can pass the sheet as an object
        Range("R" & printRow).Value = "Refused:"
        Range("S" & printRow).Value = SumRed(mySheet)
        printRow = printRow + 1
    Next mySheet

End Sub

-------------------------------------------

Function SumGreen(mySheet As Worksheet) As Long ' define the type the function is going to return

    Dim myCell As Range

    Dim counter As Long

    For Each myCell In mySheet.UsedRange.Columns("A") ' UsedRange is the range that has information

        If myCell.Font.Color = RGB(112, 173, 71) Then ' 255 is red, not green, change to whatever you need

            counter = counter + 1 ' change to counter + mycell.value if you have values and you want to sum them

        End If

    Next myCell

    ' Set the function to return the counter
    SumGreen = counter

End Function

-------------------------------------------


Function SumRed(mySheet As Worksheet) As Long ' define the type the function is going to return

    Dim myCell As Range

    Dim counter As Long

    For Each myCell In mySheet.UsedRange.Columns("A") ' UsedRange is the range that has information

        If myCell.Font.Color = 255 Then ' 255 is red, not green, change to whatever you need

            counter = counter + 1 ' change to counter + mycell.value if you have values and you want to sum them

        End If

    Next myCell

    ' Set the function to return the counter
    SumRed = counter

End Function

2 个答案:

答案 0 :(得分:0)

您的绿色不是RGB(112、173、71),请尝试

-- Table
CREATE TABLE #Numbers (
   Id int
)
INSERT INTO #Numbers
   (Id)
VALUES
   (4),
   (7),
   (9)

-- Declarations
DECLARE @select nvarchar(max)
DECLARE @from nvarchar(max)
DECLARE @stm nvarchar(max)
DECLARE @x int

-- Numbers
SELECT @x = 2

-- Statement generation 
;WITH CounterCTE as (
   SELECT 1 AS Counter
   UNION ALL
   SELECT Counter + 1   
   FROM CounterCTE
   WHERE Counter < @x
)
SELECT 
   @select = (SELECT CONCAT(N',t', Counter, N'.Id') FROM CounterCTE FOR XML PATH('')),
   @from = (SELECT CONCAT(N',#Numbers t', Counter) FROM CounterCTE FOR XML PATH(''))
SET @stm = CONCAT(
   N'SELECT ', 
   STUFF(@select, 1, 1, N''),
   N' FROM ',
   STUFF(@from, 1, 1, N'')
)   

-- Execution
PRINT @stm
EXEC sp_executesql @stm

答案 1 :(得分:0)

您需要遍历单元格,而不是范围:

Function SumGreen(mySheet As Worksheet) As Long

    Dim rng As Range
    Set rng = mySheet.UsedRange.Columns("A")
    Dim cel As Range
    Dim counter As Long

    For Each cel In rng.Cells 'add .Cells here and it works like a charm
        If myCell.Font.Color = RGB(0, 255, 0) Then
           counter = counter + 1
        End If
    Next myCell

    SumGreen = counter

End Function