SUMIF 向下行和跨列

SUMIF Down Rows & Accross Columns

SUMIF 向下行和跨列帮助

我已经成功地使用 SUMIF 来匹配列中的列表,但缺乏知识来跨列 运行 它而无需单独设置每一列 headers。感谢来自出色的 Whosebug 思维池的任何反馈。

Sub SUMIF_Down_Rows_Across_Columns()


    'Dim ws1 As Worksheet
    'Dim ws2 As Worksheet
    
    'Set ws1 = Sheets("Not_Split")
    'Set ws2 = Sheets("PNL_Large")
    
    Dim lrow1 As Long, lrow2 As Long ' first lrow controls the database, the second lrow is for the reference list column
    
    lrow1 = Range("B" & Rows.Count).End(xlUp).Row
    lrow2 = Range("K" & Rows.Count).End(xlUp).Row
    
    Dim rng1 As Range ' reference list
    Set rng1 = Range("k1")
    
    
    stJan = [l2] 'start & end dates
    endJan = [l3]
    
    stFEB = [m2] 'start & end dates
    EndFEB = [m3]
    
    stMAR = [n2] 'start & end dates
    EndMAR = [n3]
    
    stAPR = [o2] 'start & end dates
    EndAPR = [o3]
    
    stMAY = [p2] 'start & end dates
    EndMAY = [p3]
    
    stJUN = [q2] 'start & end dates
    EndJUN = [q3]
    
    stJUL = [r2] 'start & end dates
    EndJUL = [r3]
    
    Dim Dates As Range, Categories As Range, Debits As Range, Credits As Range
    
    Set Dates = Sheet6.Range("B2:B" & lrow1)
    Set Categories = Sheet6.Range("C2:C" & lrow1)
    Set Debits = Sheet6.Range("D2:D" & lrow1)
    
    ' JAN ===============================================================
    For I = 9 To lrow2
    
    JANtotals = Application.WorksheetFunction.SumIfs(Debits, Dates, ">=" & stJan, Dates, "<=" & endJan, Categories, Cells(I, 11))
    
    Cells(I, 12) = JANtotals
    
    Next I
    
    ' FEB ===============================================================
    
    For I = 9 To lrow2
    
    FEBtotals = Application.WorksheetFunction.SumIfs(Debits, Dates, ">=" & stFEB, Dates, "<=" & EndFEB, Categories, Cells(I, 11))
    
    Cells(I, 13) = FEBtotals
    
    Next I
    
    ' MAR ===============================================================
    
    For I = 9 To lrow2
    
    MARtotals = Application.WorksheetFunction.SumIfs(Debits, Dates, ">=" & stMAR, Dates, "<=" & EndMAR, Categories, Cells(I, 11))
    
    Cells(I, 14) = MARtotals
    
    Next I
    
    ' APR ===============================================================
    
    For I = 9 To lrow2
    
    APRtotals = Application.WorksheetFunction.SumIfs(Debits, Dates, ">=" & stAPR, Dates, "<=" & EndAPR, Categories, Cells(I, 11))
    
    Cells(I, 15) = APRtotals
    
    Next I
    
    ' MAY ===============================================================
    
    For I = 9 To lrow2
    
    MAYtotals = Application.WorksheetFunction.SumIfs(Debits, Dates, ">=" & stMAY, Dates, "<=" & EndMAY, Categories, Cells(I, 11))
    
    Cells(I, 16) = MAYtotals
    
    Next I
    
    ' JUN ===============================================================
    
    For I = 9 To lrow2
    
    JUNtotals = Application.WorksheetFunction.SumIfs(Debits, Dates, ">=" & stJUN, Dates, "<=" & EndJUN, Categories, Cells(I, 11))
    
    Cells(I, 17) = JUNtotals
    
    Next I
    
    ' JUL ===============================================================
    
    For I = 9 To lrow2
    
    JULtotals = Application.WorksheetFunction.SumIfs(Debits, Dates, ">=" & stJUL, Dates, "<=" & EndJUL, Categories, Cells(I, 11))
    
    Cells(I, 18) = JULtotals
    
    Next I
    
    ' AUG ===============================================================
    
    For I = 9 To lrow2
    
    AUGtotals = Application.WorksheetFunction.SumIfs(Debits, Dates, ">=" & stAUG, Dates, "<=" & EndAUG, Categories, Cells(I, 11))
    
    Cells(I, 19) = AUGtotals
    
    Next I
    
    ' SEP ===============================================================
    
    For I = 9 To lrow2
    
    SEPtotals = Application.WorksheetFunction.SumIfs(Debits, Dates, ">=" & stSEP, Dates, "<=" & EndSEP, Categories, Cells(I, 11))
    
    Cells(I, 20) = SEPtotals
    
    Next I
    
    ' OCT ===============================================================
    
    For I = 9 To lrow2
    
    OCTtotals = Application.WorksheetFunction.SumIfs(Debits, Dates, ">=" & stOCT, Dates, "<=" & EndOCT, Categories, Cells(I, 11))
    
    Cells(I, 21) = OCTtotals
    
    Next I
    
    ' NOV ===============================================================
    
    For I = 9 To lrow2
    
    NOVtotals = Application.WorksheetFunction.SumIfs(Debits, Dates, ">=" & stNOV, Dates, "<=" & EndNOV, Categories, Cells(I, 11))
    
    Cells(I, 22) = NOVtotals
    
    Next I
    
    ' DEC ===============================================================
    
    For I = 9 To lrow2
    
    DECtotals = Application.WorksheetFunction.SumIfs(Debits, Dates, ">=" & stDEC, Dates, "<=" & EndDEC, Categories, Cells(I, 11))
    
    Cells(I, 23) = DECtotals
    
    Next I

End Sub

您的代码太长,无法在此处进行审核。不必那样。下面的代码应该做同样的事情。请注意,我仅在没有数据的情况下对其进行了测试。

Option Explicit

Sub Main()
    ' 061
    
    Const Yr        As Integer = 2020
    Dim M           As Integer              ' loop counter: month
    
    For M = 1 To 12
        WriteMonthTotals M, Yr
    Next M
End Sub

Private Sub WriteMonthTotals(ByVal Mnth As Integer, _
                             ByVal Yr As Integer)
    ' 061
    
    Dim DateRng     As Range
    Dim CatRng      As Range
    Dim DrRng       As Range
    Dim StartDate   As Date         ' first day of Mnth
    Dim EndDate     As Date         ' last day of Mnth
    Dim Tmp         As Double       ' sum
    Dim C           As Long         ' target column
    Dim R           As Long         ' loop counter: Row
    
    StartDate = DateSerial(Yr, Mnth, 1)
    EndDate = DateSerial(Yr, Mnth + 1, 0)
    Debug.Print StartDate, EndDate
    C = 11 + Mnth
    
    With Sheet6
        Set DateRng = .Range(.Cells(2, "B"), .Cells(.Rows.Count, "B").End(xlUp))
        Set CatRng = DateRng.Offset(, 1)
        Set DrRng = DateRng.Offset(, 2)
        
        For R = 9 To .Cells(.Rows.Count, "K").End(xlUp).Row
            Tmp = Application.WorksheetFunction.SumIfs(DrRng, _
                                                       DateRng, ">=" & StartDate, _
                                                       DateRng, "<=" & EndDate, _
                                                       CatRng, .Cells(R, 11).Value)
            .Cells(R, C) = Tmp
        Next R
    End With
End Sub

主要区别在于代码结构。 Main 过程调用子过程 12 次,每月一次。当然,该程序保持不变。只需将输出转移到相邻的连续列,并且 Excel 非常擅长计算月份的开始和结束,一旦您告诉它您想到的是哪个月份。

在您的代码中,您有时会引用 Sheet6 并且大部分时间会引用 ActiveSheet(默认情况下,由于遗漏)。我把动作集中在 Sheet6.

将代码缩小到适合一页的大小后,我无法找出您在问题中指的是哪些列。一旦您熟悉了新结构及其功能,您或许就能自己找到解决方案。如果没有,请随时使用新代码作为基础,以便更好地解释您仍需要完成的工作。