我有一个突出显示特定数据类型的电子表格,我需要为第一个红色行上方的值单独求和,然后为红色行的LAST实例下面的值另外求和。我应该提到行是有条件格式化的,但如果我需要插入一个完全允许的辅助行。例如,不是读取返回TRUE的读取CF,而是每次存在红色行时H列可能是“x”,因此总和可以总和到第一个“x”,然后在“x”的最后一个实例之后”
输入的数据量以及突出显示的行随每个项目而变化,因此它不能是简单的单元格引用。如果重要的话,通常不止两个红色行。数字总是在黄色行中,因此值本身的单元格颜色无关紧要。
例如,对于下面的示例,我需要第一个和返回6,然后第二个和返回4.(单元格没有合并,它只是看起来那样,因为我包含在格式中snip。所有值都在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 + range(“G”&amp; x).value
End If
If x = range("b65536").End(xlUp).row Then
range("I" & x + 1).value = sumVar
End If
Next x
End Sub
我在突出显示的行上遇到“类型不匹配”错误。
使用帮助栏:
列“G”中的红色单元格(其中值为)现在用“CG”填充而不是空白。如果需要将其与添加的值分开,可将其移至“I”列。
编辑: 仍然有提供答案的问题。通配符'?'列表中的字符导致我的错误,但我想尽可能保留该功能。
以下是标准的摘要:
阿卡迪亚?不动产?信任
同意吗?房地产?CORP
亚历山大·S·INC
亚历山大?的?? INC
亚历山德里亚?真实?房地产?股票?公司
答案 0 :(得分:2)
使用辅助列,您将需要此数组公式来生成该帮助器列。 这要求标准范围值全部为小写:
=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)
答案 1 :(得分:1)
我可以看到你的示例中已经合并了单元格,但我只是使用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