【问题标题】:SUM Ranges depending on value with VBASUM 范围取决于 VBA 的值
【发布时间】:2019-02-25 13:46:52
【问题描述】:

我正在尝试使用 VBA 执行以下操作。想象一下,我有一些数据如下:

我希望我的最终结果是“BEGINDATA”和“ENDDATA”之间的每个数据的总和。所以它看起来像这样:

我的目标是获取绿色数据并写在“ENDDATA”旁边

有什么想法或建议吗?

非常感谢!!

【问题讨论】:

  • 你可以做一个宏来获得这些,但请注意,SO 不是免费的编码服务。发布您尝试过的代码,如果您在某个地方遇到问题,请在此处寻求帮助。
  • 我正在使用宏。我有一个很长的代码试图得到我的最终结果。这是我被卡住的步骤之一。我正在考虑做一个 bucle,每次找到“BEGINDATA”时选择下一行,直到找到“ENDDATA”-1 行。但我不清楚该怎么做...谢谢!

标签: excel vba sum


【解决方案1】:

试试:

Option Explicit

Sub test()

    Dim Lastrow As Long, BeginData As Long, EndData As Long, i As Long

    With ThisWorkbook.Worksheets("Sheet1")

        Lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row

        For i = 1 To Lastrow

            If .Range("A" & i).Value = "BEGINDATA" Then
                BeginData = i
            ElseIf .Range("A" & i).Value = "ENDDATA" Then
                EndData = i
            End If

            If EndData > BeginData Then
                .Range("B" & i).Value = Application.Sum(.Range("B" & BeginData + 1 & ":B" & EndData - 1))
            End If

        Next i

    End With

End Sub

另一个版本:

Option Explicit

Sub test()

    Dim Lastrow As Long, BeginData As Long, EndData As Long, i As Long

    With ThisWorkbook.Worksheets("Sheet1")

        Lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row

        For i = 1 To Lastrow

            If .Range("A" & i).Value = "BEGINDATA" Then
                BeginData = i
            ElseIf .Range("A" & i).Value = "ENDDATA" Then
                EndData = i
            End If

            If EndData > BeginData Then
                With .Range("B" & i)
                    .Value = Application.Sum(Sheet1.Range("B" & BeginData + 1 & ":B" & EndData - 1))
                    .Interior.Color = vbGreen
                End With
            End If

        Next i

    End With

End Sub

【讨论】:

  • 这简直太完美了!它完全有效:) 非常感谢
【解决方案2】:

您也可以使用Find 来实现此目的,这将比循环更快

Option Explicit
Sub Demo()
    Dim BeginData As Range, EndData As Range
    Dim FirstBeginAddress As String

    ' Update with your range
    With Sheet1.Columns(1)
        Set BeginData = .Find(what:="BEGINDATA", after:=.Cells(.Cells.Count), LookIn:=xlValues, lookat:=xlWhole)

        If Not BeginData Is Nothing Then
            FirstBeginAddress = BeginData.Address
            Set EndData = .Find("ENDDATA", after:=BeginData)
            Do
                Debug.Print "BeginAddress", BeginData.Address
                If Not EndData Is Nothing And EndData.Row > BeginData.Row Then
                    Debug.Print "EndAddress", EndData.Address
                    '' For Formula
                    EndData.Offset(0, 1).Formula = "=SUM(" & Range(BeginData.Offset(1, 1), EndData.Offset(-1, 1)).Address & ")"
                    '' For value
                    'EndData.Offset(0, 1).Value2 = Application.Sum(Range(BeginData.Offset(1, 1), EndData.Offset(-1, 1)))
                    Set EndData = .Find("ENDDATA", after:=EndData)
                Else
                    Err.Raise 998, "Demo", "Unable to find Data Footer"
                End If
                Set BeginData = .Find("BEGINDATA", after:=BeginData)
            Loop Until BeginData Is Nothing Or BeginData.Address = FirstBeginAddress
        Else
            Err.Raise 999, "Demo", "Unable to find Data Header"
        End If
    End With
End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多