在 for 循环中对 VBA/excel 中的列求和
Summing columns in VBA/excel in for loop
我正在尝试对 Sheet1 中相隔 6 个单元格的列的内容求和(即第一个是 G19:G28,第二个是 M19:M28),使用 for 循环来定义列价值。所以,我想要 G 列的总和,M 列的总和,等等,但前提是列值大于 0。然后将总和分配给 sheet 中的连续单元格 sheet =14=] 这是我目前所拥有的,但它没有用。感谢您的帮助!
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
SumIf 在 VBA
快速修复
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
我正在尝试对 Sheet1 中相隔 6 个单元格的列的内容求和(即第一个是 G19:G28,第二个是 M19:M28),使用 for 循环来定义列价值。所以,我想要 G 列的总和,M 列的总和,等等,但前提是列值大于 0。然后将总和分配给 sheet 中的连续单元格 sheet =14=] 这是我目前所拥有的,但它没有用。感谢您的帮助!
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
SumIf 在 VBA
快速修复
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