在循环中使用Sumif公式

时间:2019-02-12 13:25:09

标签: excel vba

我试图在for Loop中使用Sumif公式。我有45行(从第5行开始)和1(B列)列。我的代码涉及两个步骤:

  1. 我使用Count函数来计算此(45x1)数据集中的填充单元数。假设前四个单元格中填充了四个帐户代码。因此,Count函数返回的值为4。

  2. 我将值4用作循环的输入编号。这意味着应为四个不同的帐户代码重复四次sumif。

我也尝试了sumifs,但在四个Loop中都无法使用。

    Sub test()

    dim CountSaved1 as double
    dim Value1 as double

    countSaved1 = worksheetfunction.count(worksheets("CFmapping").Range("B5:B50")) 'Counting the number of filled cells for the loop

    For value1 = 5 to CountSaved1

    Worksheets("Cash flow Statement").Range("B4") = WorksheetFunction.SumIf(Worksheets("BS").Range("A:A"), Worksheets("CFmapping").Range("B" & Value1), Worksheets("BS").Range("G:G"))

    Next Value1

    End Sub

我要返回找到的四个值的总和。

2 个答案:

答案 0 :(得分:0)

结果将在单元格B52中发布。 试试:

Option Explicit

Sub test1()

    Dim ArrSource As Variant
    'Refer to the Sheet where your data appears
    With ThisWorkbook.Worksheets("Sheet1")
        'ArrSource represent where your data appears. Using .address(), i convert the range in R1C1 form to to be let say accepted by the Consolidate method as source. 
        ArrSource = .Range("B5:C50").Address(True, True, xlR1C1, False)
        'At range B52 i paste the results
        With .Range("B52")
            'Sources illustrate where the data appears, Xlsum used to sum the results which have the same name in LeftColumn
            .Consolidate Sources:=ArrSource, Function:=xlSum, _
                TopRow:=False, LeftColumn:=True, CreateLinks:=False
        End With

    End With

End Sub

答案 1 :(得分:0)

如果我正确地理解了您的问题,那么您可以使用一张包含帐户和金额的表格,并且希望按帐户获取总金额。 这里的第一个图像代表我的两列帐户/金额表,我一直将其命名为“ sheet1”,第二个图像代表最终结果。

enter image description here

enter image description here

这是我用来提出解决方案的vba代码。我在代码中保留了注释,以帮助您了解我如何完成我认为您正在尝试的工作。

    Sub test()

    Dim CountSaved1 As Long
    Dim sum As Long
    Dim ws As Worksheet
    sum = 0

'sort by account first.
    With ActiveWorkbook.Worksheets("Sheet1").Sort
        .SetRange Range("A4:B50")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With

' number of rows we will be summing per account.
    CountSaved1 = Range("B5").End(xlDown).Row 'WorksheetFunction.Count(Worksheets("Sheet1").Range("B5:B50")) 'Counting the number of filled cells for the loop

    For Value1 = 5 To CountSaved1

'get the current cell and compare to the next cell.
        Debug.Print (Cells(Value1, 1).Value)

        currentAccount = Cells(Value1, 1).Value 'current account cell
        nextAccount = Cells(Value1 + 1, 1).Value 'next account cell
        priorAccount = Cells(Value1 - 1, 1).Value 'prior account cell

        Debug.Print (Cells(Value1 + 1, 1).Value)

'If the current account equals the next account than sum the values
        If (currentAccount = nextAccount) Then
            sum = Cells(Value1 + 1, 2).Value + sum
        ElseIf (currentAccount <> nextAccount) And (currentAccount = priorAccount) Then
            sum = Cells(Value1 - 1, 2).Value + sum

            cellAddress = getBlankCell("Cash flow Statement")
            Worksheets("Cash flow Statement").Range(cellAddress) = currentAccount
            Worksheets("Cash flow Statement").Range(cellAddress).Offset(0, 1) = sum

' reset sum .
            sum = 0
        End If

'range, criteria, sum range
'Worksheets("Cash flow Statement").Range("B4") = WorksheetFunction.SumIf(Worksheets("BS").Range("A:A"), Worksheets("Sheet1").Range("B" & Value1), Worksheets("BS").Range("G:G"))

        Next Value1

    End Sub

    Function getBlankCell(sheet As String) As String

        Dim ws As Worksheet
        Dim foundCell As String
        Set ws = Worksheets(sheet)

        For Each cell In ws.Columns(1).Cells
            If Len(cell) = 0 Then getBlankCell = cell.Address(): Exit For
            Next cell
        End Function