【发布时间】:2015-11-23 13:22:12
【问题描述】:
我正在尝试将许多工作表合并为一张新工作表。我真的很感激任何 cmets。
问题在于这一行:
wsSrc.Range("A1", wsSrc.Range("D", lastRow)).Copy Destination:=rngDest
当我尝试运行它时会导致错误。我以前一直在使用代码将所有工作表合并到工作表摘要中,这是创建宏按钮的地方,效果很好。
Sub mcrCombine()
ActiveWorkbook.Sheets.Add.Name = "Combined" 'Create new sheet
'Definitions
Dim wsSrc As Worksheet
Dim wsDest As Worksheet
Dim rngDest As Range
Dim lastRow As Long
Dim destRow As Long
Set wsDest = Worksheets("Combined") 'Destination sheet in same Workbook
Set rngDest = wsDest.Range("B1") 'Destination cell in Combined
Application.DisplayAlerts = False 'suppress prompt worksheet delete
'loop through all sheets
For Each wsSrc In ThisWorkbook.Sheets
If wsSrc.Name <> "Summary" And wsSrc.Name <> "Combined" Then 'all sheets except summary
lastRow = wsSrc.Cells.SpecialCells(xlCellTypeLastCell).Row 'define last row
wsSrc.Range("A1", wsSrc.Range("D", lastRow)).Copy Destination:=rngDest 'copy and paste data in range
Set rngDest = rngDest.Offset(lastRow - 1) 'update destination range
wsSrc.Delete 'delete source file
End If
Next
Application.DisplayAlerts = True 'prompts back on
End Sub
【问题讨论】:
-
感谢您的反馈。我已经尝试过了,但仍然收到相同的错误。我将修改我的问题中的代码。