将数据复制到另一个工作簿中的不同范围

时间:2012-07-19 10:42:23

标签: excel excel-vba vba

下面的代码将源工作簿中的所有工作表内容复制到目标工作簿中。工作表名称完全相同。代码将来自源的数据以完全相同的顺序/范围(“A2:A700”,“& _”D2:D700,“& _”C2:C700“)复制到目标工作簿中。但是,我希望上面范围内的源数据进入目标工作簿上的不同范围(I3,k3和AC3)。任何帮助表示赞赏。

Option Explicit

    Sub seunweb()
    'this macro copies from one workbook to another

    Dim wbSource As Workbook, wbDestination As Workbook
    Dim ws As Worksheet, rng As Range
    Dim NextRow As Long, LastRow As Long

    Application.ScreenUpdating = False

        Set wbSource = Workbooks.Open("D:\test.xls")
        Set wbDestination = ThisWorkbook
    For Each ws In wbSource.Sheets

    For Each rng In ws.Range("A2:A700," & _
                                     "D2:D700," & _
                                     "C2:C700").Areas
                wbDestination.Sheets(ws.Name).Range(rng.Address).Value = rng.Value

            Next rng

        Next ws

        wbSource.Close SaveChanges:=False

        Application.ScreenUpdating = True

    End Sub

1 个答案:

答案 0 :(得分:0)

使用

之类的东西代替你的for循环
Set rng = ws.Range("A2:A700")
wbDestination.Sheets(ws.Name).Range("I3").Resize(rng.Rows.Count, rng.Columns.Count).Value = rng.Value

Set rng = ws.Range("D2:D700")
wbDestination.Sheets(ws.Name).Range("K3").Resize(rng.Rows.Count, rng.Columns.Count).Value = rng.Value

' continue this this for each source range