将总计行添加到工作簿中的每个工作表
Add Total Row To Each Worksheet in Workbook
我一直在使用这种语法将总行添加到一个工作表
With ActiveSheet
If Application.WorksheetFunction.CountA(.Cells) <> 0 Then
LastRow = .Cells.Find(What:="*", _
After:=.Range("A1"), _
LookAt:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
Else
LastRow = 1
End If
End With
Range("C" & LastRow + 1).FormulaR1C1 = "=SUM(R[-" & LastRow & "]C:R[-1]C)"
Range("C" & LastRow + 1 & ":L" & LastRow + 1).FillRight
我想将它添加到工作簿中的所有工作表中,我只需要像这样添加一个 foreach 循环
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
'code above
Next
但这不符合要求,因为我使用 With ActiveSheet
它只是将多个 "Total" 行添加到所选工作表。
如何向工作簿中的每个工作表添加总计行?
试试这个。我没有测试它。你测试代码,如果有错误让我知道,我会处理它 -
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
With ws
If Application.WorksheetFunction.CountA(.Cells) <> 0 Then
LastRow = .Cells.Find(What:="*", _
After:=.Range("A1"), _
LookAt:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
Else
LastRow = 1
End If
End With
ws.Range("C" & LastRow + 1).FormulaR1C1 = "=SUM(R[-" & LastRow & "]C:R[-1]C)"
ws.Range("C" & LastRow + 1 & ":L" & LastRow + 1).FillRight
Next
您只需确保激活每个工作表:
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
ws.activate
'code above
Next
应该做。
我一直在使用这种语法将总行添加到一个工作表
With ActiveSheet
If Application.WorksheetFunction.CountA(.Cells) <> 0 Then
LastRow = .Cells.Find(What:="*", _
After:=.Range("A1"), _
LookAt:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
Else
LastRow = 1
End If
End With
Range("C" & LastRow + 1).FormulaR1C1 = "=SUM(R[-" & LastRow & "]C:R[-1]C)"
Range("C" & LastRow + 1 & ":L" & LastRow + 1).FillRight
我想将它添加到工作簿中的所有工作表中,我只需要像这样添加一个 foreach 循环
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
'code above
Next
但这不符合要求,因为我使用 With ActiveSheet
它只是将多个 "Total" 行添加到所选工作表。
如何向工作簿中的每个工作表添加总计行?
试试这个。我没有测试它。你测试代码,如果有错误让我知道,我会处理它 -
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
With ws
If Application.WorksheetFunction.CountA(.Cells) <> 0 Then
LastRow = .Cells.Find(What:="*", _
After:=.Range("A1"), _
LookAt:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
Else
LastRow = 1
End If
End With
ws.Range("C" & LastRow + 1).FormulaR1C1 = "=SUM(R[-" & LastRow & "]C:R[-1]C)"
ws.Range("C" & LastRow + 1 & ":L" & LastRow + 1).FillRight
Next
您只需确保激活每个工作表:
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
ws.activate
'code above
Next
应该做。