【发布时间】:2015-03-19 12:04:12
【问题描述】:
我正在尝试将 Excel 中的多个工作表合并为 1 个工作表。我使用合并的工作表在同一个工作簿中运行一组报告。
- 需要合并的工作表数量各不相同 - 大约 6 到 40 个工作表。我通过在它们前面加上“Src”来标识要合并的工作表
- 在同一个工作簿中还有大约 40 个其他电子表格(根据合并工作表和其他计算得出的报告)
- 要合并的每个工作表的格式相同
我是宏的新手,所以在互联网上找到了一些代码来自动整合,但是当我有 12 个左右的工作表需要整合时,它的运行速度非常慢。
无论如何我可以加快这个宏的速度吗?
Sub CopyDataWithoutHeaders()
Dim sh As Worksheet
Dim DestSh As Worksheet
Dim Last As Long
Dim shLast As Long
Dim CopyRng As Range
Dim StartRow As Long
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
' Clear the consol worksheet except the top row with the headings
Set DestSh = ActiveWorkbook.Worksheets("All Data")
DestSh.Rows("5:" & Rows.Count).ClearContents
' Fill in the start row.ie the row in each of the source sheets that contain the data (do not include heading rows)
StartRow = 5
' Loop through all worksheets and copy the data to the
' summary worksheet if worksheet name starts with src.
For Each sh In ActiveWorkbook.Worksheets
If LCase(Left(sh.Name, 3)) = "src" Then
' Find the last row with data on the summary
' and source worksheets.
Last = LastRow(DestSh)
shLast = LastRow(sh)
' If source worksheet is not empty and if the last
' row >= StartRow, copy the range.
If shLast > 0 And shLast >= StartRow Then
'Set the range that you want to copy
Set CopyRng = sh.Range(sh.Rows(StartRow), sh.Rows(shLast))
' Test to see whether there are enough rows in the summary
' worksheet to copy all the data.
If Last + CopyRng.Rows.Count > DestSh.Rows.Count Then
MsgBox "There are not enough rows in the " & _
"summary worksheet to place the data."
GoTo ExitTheSub
End If
' This statement copies values, and formats.
CopyRng.Copy
With DestSh.Cells(Last + 1, "A")
.PasteSpecial xlPasteValues
.PasteSpecial xlPasteFormats
Application.CutCopyMode = False
End With
End If
End If
Next
ExitTheSub:
Application.GoTo DestSh.Cells(1)
' AutoFit the column width in the summary sheet.
DestSh.Columns.AutoFit
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
End Sub
Function LastRow(sh As Worksheet)
On Error Resume Next
LastRow = sh.Cells.Find(What:="*", _
After:=sh.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
On Error GoTo 0
End Function
Function LastCol(sh As Worksheet)
On Error Resume Next
LastCol = sh.Cells.Find(What:="*", _
After:=sh.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Column
On Error GoTo 0
End Function
【问题讨论】:
-
您可以关闭计算和屏幕更新。
-
尝试切断一些像“StartRow”这样的声明,因为它的值是固定的,它不是一个变量,所以只要在她出现的地方放5,如果vlue没有沿着代码改变,那里不需要为它创建一个变量,它不会变得更快,但它确实有帮助
-
@YgorYansz 实际上,在较长的过程中,使用这样的常量或变量是有意义的 - 如果将来需要更新该值,那么您只需在此行上更改一次,而不是查找每行并替换值。
-
@SO 我知道,我的意思是在这个特定的情况下,因为它只出现两次并且它从不随代码改变,所以它只是针对这个特定的情况