如何在特定范围的每一行右侧的单元格中执行一个函数?
How to execute a function in the cell on the right of every row of a specific range?
早些时候,我试图对正下方单元格中范围内的每一列求和,并成功获得了帮助。我只是想修改我收到的代码,对每一行都做同样的事情,并在右边的列中得到结果
Sub miine()
Dim a, i As Integer, Data, sums As Range
a = InputBox("How many cells for the square?")
Set Data = Range(ActiveCell.Offset(0, 0), ActiveCell.Offset(a - 1, a - 1))
Set sums = Range(ActiveCell.Offset(a, 0), ActiveCell.Offset(a, a - 1))
For Each cell In sums
cell.Value = "=SUM(" & Split(cell.Address, "$")(1) & cell.Row - a & ":" & Split(cell.Address, "$")(1) & cell.Row - 1 & ")"
Next
Set sum = Range(ActiveCell.Offset(0, a), ActiveCell.Offset(a - 1, a))
For each cell in sum
cell.Value = "=SUM(" & cell.Column - a & Split(cell.Address, "$")(2) & ":" & cell.Column - 1 & Split(cell.Address, "$")(2) & ")"
End Sub
第二部分不起作用,我在右边得到了总和,但是为总和选择的范围看起来像 Range(11:41) 而不是 Range("A1:D1")。提前致谢
Cell.Address是这样的:“$A
”;函数 Split
按符号 $
拆分,因此字母 A
将包含在数组的位置 (1)
而行号 1
位于该位置(2)
。这就是为什么你的:
Split(cell.Address, "$")(2)
返回的是数字而不是字母。你想做的大概是:
Set sum = Range(ActiveCell.Offset(0, a), ActiveCell.Offset(a - 1, a))
For each cell in sum
cell.Value = "=SUM(" & Split(cell.Offset(0,-a).Address,"$")(1) & cell.Row & ":" & Split(cell.Offset(0,-1).Address,"$")(1) & cell.Row & ")"
Next cell
早些时候,我试图对正下方单元格中范围内的每一列求和,并成功获得了帮助。我只是想修改我收到的代码,对每一行都做同样的事情,并在右边的列中得到结果
Sub miine()
Dim a, i As Integer, Data, sums As Range
a = InputBox("How many cells for the square?")
Set Data = Range(ActiveCell.Offset(0, 0), ActiveCell.Offset(a - 1, a - 1))
Set sums = Range(ActiveCell.Offset(a, 0), ActiveCell.Offset(a, a - 1))
For Each cell In sums
cell.Value = "=SUM(" & Split(cell.Address, "$")(1) & cell.Row - a & ":" & Split(cell.Address, "$")(1) & cell.Row - 1 & ")"
Next
Set sum = Range(ActiveCell.Offset(0, a), ActiveCell.Offset(a - 1, a))
For each cell in sum
cell.Value = "=SUM(" & cell.Column - a & Split(cell.Address, "$")(2) & ":" & cell.Column - 1 & Split(cell.Address, "$")(2) & ")"
End Sub
第二部分不起作用,我在右边得到了总和,但是为总和选择的范围看起来像 Range(11:41) 而不是 Range("A1:D1")。提前致谢
Cell.Address是这样的:“$A
”;函数 Split
按符号 $
拆分,因此字母 A
将包含在数组的位置 (1)
而行号 1
位于该位置(2)
。这就是为什么你的:
Split(cell.Address, "$")(2)
返回的是数字而不是字母。你想做的大概是:
Set sum = Range(ActiveCell.Offset(0, a), ActiveCell.Offset(a - 1, a))
For each cell in sum
cell.Value = "=SUM(" & Split(cell.Offset(0,-a).Address,"$")(1) & cell.Row & ":" & Split(cell.Offset(0,-1).Address,"$")(1) & cell.Row & ")"
Next cell