用于选择活动单元格的单独自动求和偏移量或单独范围 Excel VBA
Separate Autosum Offsets or Individual Ranges For Selecting Active Cells Excel VBA
我可能已经破坏了那个标题,但我想做的是自动求和(通过下面的宏)仅基于下面的偏移量的某些列。
所以在找到 "CCOIL"、"COIL" 或 "DCOIL" 的地方,它移动 4 列,求和,然后移动 6 列并求和(总共 10 个不同的列,最终).
然而,对于 "RANGE",它会在下面的示例中为 4 和 10 之间的每一列添加一个小计,但是我只希望某些列有小计。那么有没有办法 select 具有多个偏移量的多个单元格?
所以像
((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
示例数据集
也许:
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
编辑 - 我想我找到了使用 "union"
的替代方法
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
我可能已经破坏了那个标题,但我想做的是自动求和(通过下面的宏)仅基于下面的偏移量的某些列。
所以在找到 "CCOIL"、"COIL" 或 "DCOIL" 的地方,它移动 4 列,求和,然后移动 6 列并求和(总共 10 个不同的列,最终).
然而,对于 "RANGE",它会在下面的示例中为 4 和 10 之间的每一列添加一个小计,但是我只希望某些列有小计。那么有没有办法 select 具有多个偏移量的多个单元格?
所以像
((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
示例数据集
也许:
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
编辑 - 我想我找到了使用 "union"
的替代方法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