对空白单元格上方的行求和,直到上一个空白单元格

Summing rows above blank cell until previous blank cell

您好!

我正在尝试构建一个宏,它在单列的范围内找到一个空白单元格,并对这个空白单元格和前一个空白单元格之间的所有单元格求和。

我在网上搜索了很多次,虽然其他人也问过这个问题,但我发现他们的答案对我的情况没有特别帮助,因为我需要这个来处理 3500 多行。

例如:

4  
3  
4  
BLANK 1  
2  
5  
7  
1  
BLANK 2  
1  
4  
BLANK 3

在这种情况下,名为 "BLANK 1" 的单元格将是前 3 行的总和:4+3+4=11
"Blank 2" 将是 15 等等。

范围是"G8:G3561".

编辑

要获得快速答案,请参阅 Mr_Nitrogen 的答案。它工作得很漂亮! 但是,由于我是 VBA 的新手并且一般来说我不知道​​代码是如何工作的或为什么工作。

我非常渴望更好地理解 VBA,这就是为什么我要继续这个话题(如果允许的话)。
此外,我想为评论者提供证据,证明我自己确实从事过此工作,并且我更愿意构建自己的代码。

了解我的代码为何有效或无效对我来说很重要,这就是为什么我希望您仍然愿意帮助我开发自己的代码。

我终于找到了一种对我来说合乎逻辑的方法。我知道这不是最简单的方法,但我想知道它是否可行。

我写了下面的代码。

Sub Sum_storage()
Dim rng As Range
Dim cell As Range
Dim cell2 As Range
Dim cell3 As Range

Range("G8").End(xlDown).Offset(1, 0).Select
Set cell = Selection
cell.Value = "temp" 'Finds the first blank cell in column G _
                     and creates a temporary value in order _
                     to find the second blank cell

Range("G8").End(xlDown).Offset(1, 0).Select
Set cell2 = Selection

cell.Offset(1, 0).Select
Set cell3 = Selection  'The range i need to sum can _
                        now be described as "cell3:cell2"

Set rng = Range(Range("cell3"), Range("cell2")) 'The code works until this point
cell2.Value = WorksheetFunction.Sum(rng)

我的想法是定义我想用多个变量求和的范围。
我的问题是尝试引用这些变量(并以比使用 .Offset 更简单的方式设置它们)。

是否根本不可能根据两个先前设置的范围来设置范围 (rng)?

如果可能的话,我的下一步是创建某种循环,使它适用于所有 3500 多行。

用 for 循环做起来并不复杂,可能类似于

lastrow = Cells(Rows.Count, "G").End(xlUp).Row
firstrow = 8
TempTotal = 0
for x = firstrow to lastrow + 1
    If Cells(x, "G") <> "" Then
        TempTotal = TempTotal + Cells(x, "G")
    Else:   Cells(x, "G") = TempTotal
            Cells(x, "G").Interior.ColorIndex = 4
            TempTotal = 0
    End if
    Next x

我进行了编辑以使其更简单一些

代码逻辑:

  1. 用列 "G"
  2. 中的数据定义最后一行
  3. 逐个单元格向下移动直到该行
  4. 如果单元格中有值,将其添加到临时总计中。
  5. 如果为空,则插入临时合计,将tempTotal重置为零

    1. This is the first line, in which we select the very last last cell in column "G", use use End(xlup) on it to get to the last cell with data and use .row to get the row number of that cell

    2. Set up a For loop, which runs the code between "for" and "next x" lines repeatedly while incrementing the value of x from "firstrow" to "lastrow + 1" each time it repeats ( so if firsrow is 1 and lastrow is 100) then it will run the code 100 times with x = 1,2,3,4,5 etc.)

    3. this is the "if" statement, "<>" means does not equal, so we are saying if the cell on row x, col "G" is not equal to "" which is an empty string (or nothing) then we do the next line (add its value to tempTotal)
    4. if the "If" statement isnt true (if the cell is blank) then we do what is under the "Else" and make that cell equal to TempTotal, change its color to green (4 is a colorcode, they go between 1 and 50), and reset the temptotal to 0.

您使用 End(xlDown) 的方法是正确的。

这应该比遍历所有单元格更快,因为它跳转到下一个空单元格并通过 WorksheetFunction.Sum 求和。

Option Explicit

Public Sub DoMyStuff()
    Dim ws As Worksheet
    Set ws = Worksheets("Tabelle8") 'define your worksheet here

    Dim FirstCell As Range
    Set FirstCell = ws.Range("G8")

    Dim VeryLastCell As Range 'get very last cell as stop criteria
    Set VeryLastCell = ws.Cells(ws.Rows.Count, "G").End(xlUp)

    Do
        Dim LastCell As Range
        If FirstCell.Offset(1) = vbNullString Then 'test if there is only one cell to sum
            Set LastCell = FirstCell
        Else
            Set LastCell = FirstCell.End(xlDown)
        End If

        With LastCell.Offset(1, 0) 'this is the cell we want to write the sum
            .Value = Application.WorksheetFunction.Sum(ws.Range(FirstCell, LastCell))
            .Interior.Color = RGB(255, 0, 0)
        End With

        Set FirstCell = LastCell.Offset(2, 0)

    Loop While FirstCell.Row < VeryLastCell.Row
End Sub