总和范围到某个单元格颜色或值之后?
Sum range up to, and after, a certain cell color or value?
我有一个突出显示特定类型数据的电子表格,我需要对第一个红色行上方的值单独求和,然后对红色行的最后一个实例下方的值进行另一个求和。我应该提到这些行是有条件格式化的,但如果我需要插入一个辅助行,那是完全允许的。例如,不是读取读取的 CF return 为真,在 H 列中每次出现红色行时都可能是 "x",因此总和可能等于第一个 "x"然后在 "x".
的最后一个实例之后
输入的数据量,突出显示的行随着每个项目的不同而不同,所以不能是简单的单元格引用。如果重要的话,通常不止两行红色。数字始终位于黄色行中,因此值本身的单元格颜色无关紧要。
例如,对于下面的示例,我需要第一个总和为 return 6,然后第二个总和为 return 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 + range("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
我在突出显示的行上收到 "type mismatch" 错误。
有辅助列:
第 "G" 列(值所在的位置)中的红色单元格现在用 "CG" 填充,而不是空白。如果需要将其与附加值分开,可以将其移至 "I" 列。
编辑:
提供的答案仍然存在问题。通配符 '?'列表中的字符导致了我的错误,但我想尽可能保留该功能。
这里是标准的片段:
acadia?realty?trust
同意吗?realty?corp
亚历山大?s?公司
亚历山大?s??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
编辑
使用 "helper":
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: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: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)
我有一个突出显示特定类型数据的电子表格,我需要对第一个红色行上方的值单独求和,然后对红色行的最后一个实例下方的值进行另一个求和。我应该提到这些行是有条件格式化的,但如果我需要插入一个辅助行,那是完全允许的。例如,不是读取读取的 CF return 为真,在 H 列中每次出现红色行时都可能是 "x",因此总和可能等于第一个 "x"然后在 "x".
的最后一个实例之后输入的数据量,突出显示的行随着每个项目的不同而不同,所以不能是简单的单元格引用。如果重要的话,通常不止两行红色。数字始终位于黄色行中,因此值本身的单元格颜色无关紧要。
例如,对于下面的示例,我需要第一个总和为 return 6,然后第二个总和为 return 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 + range("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
我在突出显示的行上收到 "type mismatch" 错误。
有辅助列:
第 "G" 列(值所在的位置)中的红色单元格现在用 "CG" 填充,而不是空白。如果需要将其与附加值分开,可以将其移至 "I" 列。
编辑: 提供的答案仍然存在问题。通配符 '?'列表中的字符导致了我的错误,但我想尽可能保留该功能。
这里是标准的片段:
acadia?realty?trust
同意吗?realty?corp
亚历山大?s?公司
亚历山大?s??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
编辑
使用 "helper":
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: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: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)