-1
我有一個合併工作表的宏。當條目被添加到單個工作表時,我希望刷新組合工作表。清除工作表內容
我在其他工作表上引用組合工作表的公式。
在組合代碼中,如果存在組合紙張,則會刪除組合紙張,然後再次添加。這混淆了所有的公式引用。我想刪除刪除並重新添加組合工作表的部分,而是清除工作表內容,然後合併數據。
這裏是我到目前爲止的代碼。
Sub CopyRangeFromMultiWorksheets()
Dim sh As Worksheet
Dim DestSh As Worksheet
Dim Last As Long
Dim CopyRng As Range
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
'Delete the sheet "CombinedReport" if it exist
Application.DisplayAlerts = False
On Error Resume Next
ActiveWorkbook.Worksheets("CombinedReport").Delete
On Error GoTo 0
Application.DisplayAlerts = True
'Add a worksheet with the name "CombinedReport"
Set DestSh = ActiveWorkbook.Worksheets.Add
DestSh.name = "CombinedReport"
'loop through all worksheets and copy the data to the DestSh
For Each sh In ActiveWorkbook.Sheets(Array("UCDP", "UCD", "ULDD", "PE-WL", "eMortTri", "eMort", "EarlyCheck", "DU", "DO", "CDDS", "CFDS"))
Last = DestSh.Cells.SpecialCells(xlCellTypeLastCell).Row
'Fill in the range that you want to copy
Set CopyRng = sh.UsedRange
Set CopyRng = CopyRng.Offset(1, 0).Resize(CopyRng.Rows.Count - 1, CopyRng.Columns.Count)
'Test if there enough rows in the DestSh to copy all the data
If Last + CopyRng.Rows.Count > DestSh.Rows.Count Then
MsgBox "There are not enough rows in the Destsh"
GoTo ExitTheSub
End If
'This example copies values/formats, if you only want to copy the
'values or want to copy everything look at the example below this macro
CopyRng.Copy
With DestSh.Cells(Last + 1, "A")
.PasteSpecial xlPasteValues
.PasteSpecial xlPasteFormats
Application.CutCopyMode = False
End With
Next
ExitTheSub:
Application.Goto DestSh.Cells(1)
'AutoFit the column width in the DestSh sheet
DestSh.Columns.AutoFit
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
End Sub
如果您已經打開了一個問題,那麼發佈一個副本而不提及現有的帖子是有點不考慮的... –