我可能已经取消了该标题,但是我想做的是根据下面的偏移量仅通过某些列自动求和(通过下面的宏)。
因此,在找到“ CCOIL”,“ COIL”或“ DCOIL”的位置,它将四列移动,求和,然后将六列移动并求和(最终总共十个不同的列)。
但是,在下面的示例中,使用“ RANGE”时,它会向4到10之间的每一列添加小计,但是我只希望某些列具有小计。那么有没有办法选择具有多个偏移量的多个像元?
类似
((Cell.Offset(0, 4)), (Cell.Offset(0, 6)), Cell.Offset(0,8)), Cell.Offset(0,10))).Activate
到目前为止我的代码:
For Each Cell In Range("E2:E" & findLastRow(.Range("A2")))
If Cell.Value = "CCOIL" Or Cell.Value = "COIL" Or Cell.Value = "DCOIL" Then
Range((Cell.Offset(0, 4)), (Cell.Offset(0, 10))).Activate
ActiveCell.Formula = _
"=SUM(" & Range(ActiveCell.Offset(-1, 0), ActiveCell.Offset(-1, 0).End(xlUp)).Address & ")"
End If
Next Cell
示例数据集
答案 0 :(得分:3)
也许:
Dim rngX As Range, rngY As Range
For Each cell In Range("E2:E1000") 'example of range
If cell.Value = "CCOIL" Or cell.Value = "COIL" Or cell.Value = "DCOIL" Then
'Range((cell.Offset(0, 4)), (cell.Offset(0, 10))).Activate
Set rngX = cell.Offset(0, 4)
rngX.Formula = "=SUM(" & rngX.Offset(-1, 0).Address & ":" & rngX.Offset(-1, 0).End(xlUp).Address & ")"
Set rngY = cell.Offset(0, 10)
rngY.Formula = "=SUM(" & rngY.Offset(-1, 0).Address & ":" & rngY.Offset(-1, 0).End(xlUp).Address & ")"
End If
Next cell
答案 1 :(得分:0)
编辑-我想我找到了使用“联盟”的替代方法
For Each Cell In Range("E2:E" & findLastRow(.Range("A2")))
If Cell.Value = "CCOIL" Or Cell.Value = "COIL" Or Cell.Value = "DCOIL" Then
Union(Cell.Offset(0, 4), Cell.Offset(0, 6), Cell.Offset(0, 8), Cell.Offset(0, 10)).Select
Selection.Formula = _
"=SUM(" & Range(Selection.Offset(-1, 0), Selection.Offset(-1, 0).End(xlUp)).Address(False, False) & ")"
End If
Next Cell