Excelmacros:用于组合来自特定工作表的数据并刷新的代码?

我有一本有20张左右的工作簿。 这些工作表中有11个是基于数据的外部连接,当添加条目并刷新工作簿时,外部连接会更新并添加数据行。 其他工作表是表格和图表的工作表。 我正在尝试使用vba将11个基于数据的工作表中的数据合并到一个工作表中,然后还将合并的工作表刷新并更新,因为将更多条目添加到单个工作表中。

我有一个代码,结合具体的11张,但是,我需要在刷新部分的帮助。 我试着添加一个可以重新运行代码的button,但是它会删除并重新添加一个组合表单,这会混淆我公式中的所有引用。 我希望有人能够重新编写一些代码,以便组合的工作表不会被删除,并且不需要添加新的工作表,这样数据就可以更新到组合工作表中而不用拧上参考。

谢谢。

这是我到目前为止的代码…

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").Cells.ClearContents 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