【问题标题】:Copying Multiple Ranges and Sheets复制多个范围和工作表
【发布时间】:2018-06-19 13:30:17
【问题描述】:

我正在尝试将工作簿中的多个范围复制到一张工作表中。我曾尝试使用 MyMultipleRange,但不断收到“object_'Global' 的方法 'Union' 失败的消息。

Function WorkbookName() As String
    WorkbookName = ThisWorkbook.Name
End Function

Sub dataimport()
    Dim i As Integer
    Dim Data

    Workbooks(WorkbookName).Activate
    Sheets("Input").Select
    Datapath = Cells(15, 4)
    Data = Cells(15, 3)

    Application.Workbooks.Open (Datapath)

    Dim r1, r2, myMultipleRange As Range
    Set r1 = Sheets("Sheet1").Range("A1:Ak518")
    Set r2 = Sheets("Sheet2").Range("B2:J10")
    Set myMultipleRange = Union(r1, r2)

    Workbooks(WorkbookName).Activate
    Sheets("Sheet5").Select
    Range("A1:Ak600").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False


     Workbooks(Data).Close savechanges:=False

End Sub

【问题讨论】:

  • 你不能在不同的工作表上使用联合。
  • 那么最好的方法是什么?我需要从工作表 1 和 2 中复制范围。
  • 你试过找吗?例如。 stackoverflow.com/questions/25801941/…

标签: excel range copying vba


【解决方案1】:

我认为这会满足您的要求。

Set CopyRng = sh.Range("A1:G1")

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 "RDBMergeSheet" if it exist
    Application.DisplayAlerts = False
    On Error Resume Next
    ActiveWorkbook.Worksheets("RDBMergeSheet").Delete
    On Error GoTo 0
    Application.DisplayAlerts = True

    'Add a worksheet with the name "RDBMergeSheet"
    Set DestSh = ActiveWorkbook.Worksheets.Add
    DestSh.Name = "RDBMergeSheet"

    'loop through all worksheets and copy the data to the DestSh
    For Each sh In ActiveWorkbook.Worksheets
        If sh.Name <> DestSh.Name Then

            'Find the last row with data on the DestSh
            Last = LastRow(DestSh)

            'Fill in the range that you want to copy
            Set CopyRng = sh.Range("A1:G1")

            '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

            'Optional: This will copy the sheet name in the H column
            DestSh.Cells(Last + 1, "H").Resize(CopyRng.Rows.Count).Value = sh.Name

        End If
    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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多