总和范围达到某个单元格的颜色或值之后?

我有一个电子数据表,突出显示特定types的数据,我需要单独的总和为第一个红色行上面的值,然后另一个总和为红色行的LAST实例下面的值。 我应该提到行是有条件格式的,但是如果我需要一个辅助行被插入,这是完全可以允许的。 例如,读取读取的CF返回TRUE,而不是读取CF返回TRUE,在H列中每次有红色的行可以是“x”,所以总和可以总结为第一个“x”,然后在“x ”。

input的数据量和突出显示的行随着每个项目而变化,因此它不能是一个简单的单元格引用。 如果有的话,通常只有两个红色的行。 数字总是在黄色的行中,所以这些值本身的单元格颜色是不相关的。

例如,对于下面的例子,我需要第一个总和返回6,然后第二个总和返回4.(单元格不合并,因为我包含在剪报中的格式,这只是看起来这样。值在G列)

颜色示例

颜色:

Sub findRedsAndSum() redCount = 0 For x = 1 To range("b65536").End(xlUp).row 'find last row If range("G" & x).value = "CG" Then 'find red cells redCount = redCount + 1 End If Next x redCountAgain = 0 For x = 1 To range("b65536").End(xlUp).row 'find last row If range("G" & x).value = "CG" And redCountAgain = 0 Then range("I" & x - 1).value = sumVar sumVar = 0 redCountAgain = redCountAgain + 1 ElseIf range("G" & x).DisplayFormat.Interior.ColorIndex = 3 Then redCountAgain = redCountAgain + 1 sumVar = 0 End If If redCountAgain = redCount And range("G" & x).value <> "CG" Then sumVar = sumVar + range("G" & x).value End If If redCountAgain = 0 Then 

sumVar = sumVar +范围(“G”&x).value

  End If If x = range("b65536").End(xlUp).row Then range("I" & x + 1).value = sumVar End If Next x End Sub 

突出显示的行上出现“types不匹配”错误。

与帮手列:

“G”列中的红色单元(其中的值)现在用“CG”填充而不是空白。 如果需要将其与增值分离,那么可以将其移动到“I”列。

编辑:仍然有提供的答案的问题。 通配符“?” 列表中的字符导致我的错误,但我想保留该function,如果可能的话。

这是一个标准的片段:

阿卡迪亚?房地产?信任
同意吗?房地产?CORP
亚历山大·S·INC
亚历山大?的?? INC
亚历山大?真的吗?房地产?股票?INC

我可以看到你已经在你的例子中合并了单元格,但是我只是使用列A给出了代码
在这里输入图像说明

 Sub findRedsAndSum() redCount = 0 For x = 1 To Range("a65536").End(xlUp).Row 'find last row If Range("a" & x).Interior.ColorIndex = 3 Then 'find red cells redCount = redCount + 1 End If Next x redCountAgain = 0 For x = 1 To Range("a65536").End(xlUp).Row 'find last row If Range("a" & x).Interior.ColorIndex = 3 And redCountAgain = 0 Then Range("b" & x - 1).Value = sumVar sumVar = 0 redCountAgain = redCountAgain + 1 ElseIf Range("a" & x).Interior.ColorIndex = 3 Then redCountAgain = redCountAgain + 1 sumVar = 0 End If If redCountAgain = redCount And Range("a" & x).Interior.ColorIndex <> 3 Then sumVar = sumVar + Range("a" & x).Value End If If redCountAgain = 0 Then sumVar = sumVar + Range("a" & x).Value End If If x = Range("a65536").End(xlUp).Row Then Range("b" & x + 1).Value = sumVar End If Next x End Sub 

编辑

使用“帮手”:
在这里输入图像说明

 Sub findRedsAndSum() redCount = 0 For x = 1 To Range("a65536").End(xlUp).Row 'find last row If Range("a" & x).Value = "this is red" Then 'find red cells redCount = redCount + 1 End If Next x redCountAgain = 0 For x = 1 To Range("a65536").End(xlUp).Row 'find last row If Range("a" & x).Value = "this is red" And redCountAgain = 0 Then Range("b" & x - 1).Value = sumVar sumVar = 0 redCountAgain = redCountAgain + 1 ElseIf Range("a" & x).Interior.ColorIndex = 3 Then redCountAgain = redCountAgain + 1 sumVar = 0 End If If redCountAgain = redCount And Range("a" & x).Value <> "this is red" Then sumVar = sumVar + Range("a" & x).Value End If If redCountAgain = 0 Then sumVar = sumVar + Range("a" & x).Value End If If x = Range("a65536").End(xlUp).Row Then Range("b" & x + 1).Value = sumVar End If Next x End Sub 

使用助手列,你会希望这个数组公式,以帮助列。 这要求标准范围值全部小写

 =IF(AND(SUM(LEN(B2)-LEN(SUBSTITUTE(LOWER(B2),Criteria,"")))>0,ROW()<MATCH(TRUE,ISNUMBER(SEARCH("capital gain",$B$1:INDEX(B:B,MATCH("ZZZ",B:B)))),0)),1,IF(AND(SUM(LEN(B2)-LEN(SUBSTITUTE(LOWER(B2),Criteria,"")))>0,ROW() > MATCH(2,IF(ISNUMBER(SEARCH("capital gain",$B$1:INDEX(B:B,MATCH("ZZZ",B:B)))),1))),2,0)) 

这会为这些数字加上1,之后加上2。

作为一个数组公式,在退出编辑模式时,需要使用Ctrl-Shift-Enter而不是Enter来确认。 如果正确完成,Excel将在公式周围放置{}

那么这是一个快速SUMIF()(我把我的公式在J栏)

 =SUMIF(J:J,1,F:F) 

 =SUMIF(J:J,2,F:F) 

![![![在这里输入图片描述