添加行时自动更改功能范围
change the range of function automatically when rows are added
[1
A1单元格写=sum(A2:A11),A2:A11写随机数。然后我删除了一些行,然后 A1 单元格的范围自动更改。但我不明白为什么当我添加新行并插入新值时范围不会自动更改。我怎样才能让它自动改变?我必须使用 vba 才能执行此操作吗?
一部作品sheet变更事件:监控栏目数据变更
- 我个人会同意 JvdV 在评论中的建议。
- 每次手动更改单元格时,例如在
A
列中,它将检查公式
=SUM(A2:ALastRow)
在单元格 A1
中,如果不正确,它将用正确的覆盖它。
- 您可以将其用于多个 non-adjacent 列,例如
"A,C:D,E"
.
- 无需 运行。只需将代码复制到适当的 sheet 模块中,例如
Sheet1
并退出 Visual Basic 编辑器。
Sheet 模块例如Sheet1
(不是标准模块,例如 Module1
)
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
UpdateFirstRowFormula Target, "A"
End Sub
Private Sub UpdateFirstRowFormula( _
ByVal Target As Range, _
ByVal ColumnList As String)
On Error GoTo ClearError
Dim ws As Worksheet: Set ws = Target.Worksheet
Dim Cols() As String: Cols = Split(ColumnList, ",")
Application.EnableEvents = False
Dim irg As Range, arg As Range, crg As Range, lCell As Range
Dim n As Long
Dim Formula As String
For n = 0 To UBound(Cols)
With ws.Columns(Cols(n))
With .Resize(.Rows.Count - 1).Offset(1)
Set irg = Intersect(.Cells, Target.EntireColumn)
End With
End With
If Not irg Is Nothing Then
For Each arg In irg.Areas
For Each crg In arg.Columns
Set lCell = crg.Find("*", , xlFormulas, , , xlPrevious)
If Not lCell Is Nothing Then
Formula = "=SUM(" & crg.Cells(1).Address(0, 0) & ":" _
& lCell.Address(0, 0) & ")"
With crg.Cells(1).Offset(-1)
If .Formula <> Formula Then .Formula = Formula
End With
End If
Next crg
Next arg
Set irg = Nothing
End If
Next n
SafeExit:
If Not Application.EnableEvents Then Application.EnableEvents = True
Exit Sub
ClearError:
Debug.Print "Run-time error '" & Err.Number & "': " & Err.Description
Resume SafeExit
End Sub
使用嵌套函数如下:
=SUM(偏移量(A2,COUNTA(A2:A26)))
[
A1单元格写=sum(A2:A11),A2:A11写随机数。然后我删除了一些行,然后 A1 单元格的范围自动更改。但我不明白为什么当我添加新行并插入新值时范围不会自动更改。我怎样才能让它自动改变?我必须使用 vba 才能执行此操作吗?
一部作品sheet变更事件:监控栏目数据变更
- 我个人会同意 JvdV 在评论中的建议。
- 每次手动更改单元格时,例如在
A
列中,它将检查公式=SUM(A2:ALastRow)
在单元格A1
中,如果不正确,它将用正确的覆盖它。 - 您可以将其用于多个 non-adjacent 列,例如
"A,C:D,E"
. - 无需 运行。只需将代码复制到适当的 sheet 模块中,例如
Sheet1
并退出 Visual Basic 编辑器。
Sheet 模块例如Sheet1
(不是标准模块,例如 Module1
)
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
UpdateFirstRowFormula Target, "A"
End Sub
Private Sub UpdateFirstRowFormula( _
ByVal Target As Range, _
ByVal ColumnList As String)
On Error GoTo ClearError
Dim ws As Worksheet: Set ws = Target.Worksheet
Dim Cols() As String: Cols = Split(ColumnList, ",")
Application.EnableEvents = False
Dim irg As Range, arg As Range, crg As Range, lCell As Range
Dim n As Long
Dim Formula As String
For n = 0 To UBound(Cols)
With ws.Columns(Cols(n))
With .Resize(.Rows.Count - 1).Offset(1)
Set irg = Intersect(.Cells, Target.EntireColumn)
End With
End With
If Not irg Is Nothing Then
For Each arg In irg.Areas
For Each crg In arg.Columns
Set lCell = crg.Find("*", , xlFormulas, , , xlPrevious)
If Not lCell Is Nothing Then
Formula = "=SUM(" & crg.Cells(1).Address(0, 0) & ":" _
& lCell.Address(0, 0) & ")"
With crg.Cells(1).Offset(-1)
If .Formula <> Formula Then .Formula = Formula
End With
End If
Next crg
Next arg
Set irg = Nothing
End If
Next n
SafeExit:
If Not Application.EnableEvents Then Application.EnableEvents = True
Exit Sub
ClearError:
Debug.Print "Run-time error '" & Err.Number & "': " & Err.Description
Resume SafeExit
End Sub
使用嵌套函数如下: =SUM(偏移量(A2,COUNTA(A2:A26)))