1

我正在尝试使用 for 循环将 Sheet1 中相隔 6 个单元格的列的内容相加(即第一个是 G19:G28,第二个是 M19:M28)来定义列值。所以,我想要 G 列的总和、M 列的总和等等,但前提是列值大于 0。然后将总和分配给名为“计算器”的工作表中的连续单元格。这是我到目前为止所拥有的,但它不起作用。任何帮助表示赞赏!

    For m = 1 To 20
    totdamage = Application.WorksheetFunction.SumIf(Worksheets("Sheet1").Range(Cells(19, (6 * 
    m) + 1), Cells(28, (6 * m) + 1)), ">0", Worksheets("Sheet1").Range(Cells(19, (6 * m) + 
    1).Value, Cells(28, (6 * m) + 1).Value)):
    Worksheets("Calculator").Cells(m + 6, 31).Value = totdamage
    Next m 
4

1 回答 1

0

VBA 中的 SumIf

快速修复

Option Explicit

Sub SumToCalculator()
    
    Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
    Dim sws As Worksheet: Set sws = wb.Worksheets("Sheet1")
    Dim dws As Worksheet: Set dws = wb.Worksheets("Calculator")
    
    Dim srg As Range
    Dim m As Long
    Dim TotDamage As Double
    
    For m = 1 To 20
        Set srg = sws.Rows("19:28").Columns("G").Offset(, (m - 1) * 6)
        TotDamage = Application.SumIf(srg, ">0", srg)
        dws.Cells(m + 6, "AE").Value = TotDamage ' first in 'AE7'
    Next m

End Sub
于 2021-06-14T17:41:44.677 回答