vba代码 实现在excel总表中将非总表的其他所有工作表数据都汇总到总表中
时间: 2024-03-10 14:48:44 浏览: 58
你可以使用 VBA 代码来实现将非总表的其他所有工作表数据都汇总到总表中。以下是示例代码:
```
Sub ConsolidateData()
Dim ws As Worksheet
Dim lastRow As Long
Dim totalRows As Long
Dim i As Long
' Set the totalRows variable to be the total number of rows in the destination worksheet
totalRows = ThisWorkbook.Worksheets("总表").Range("A" & Rows.Count).End(xlUp).Row
' Loop through all worksheets in the workbook except the destination worksheet
For Each ws In ThisWorkbook.Worksheets
If ws.Name <> "总表" Then
' Find the last row of data on the current worksheet
lastRow = ws.Range("A" & Rows.Count).End(xlUp).Row
' Copy the data from the current worksheet to the destination worksheet
ws.Range("A2:E" & lastRow).Copy _
Destination:=ThisWorkbook.Worksheets("总表").Range("A" & totalRows + 1)
' Update the totalRows variable to include the new data from the current worksheet
totalRows = ThisWorkbook.Worksheets("总表").Range("A" & Rows.Count).End(xlUp).Row
End If
Next ws
End Sub
```
这个代码会循环遍历工作簿中的所有工作表,跳过名为“总表”的工作表。然后,它会找到每个工作表中的最后一行数据,将该工作表的数据复制到“总表”工作表的下一行。最后,它会更新“总表”工作表的总行数,以便下一个工作表的数据可以添加到正确的位置。
阅读全文
相关推荐
















