首页 > 解决方案 > 从具有相同目标的不同工作表中复制粘贴

问题描述

我必须执行相同的复制粘贴,但对于不同工作表(4、6、8 等)中的更多列,不像 j...

这些行正在使用 arrRows(感谢 Tinman)

子 GHM()

Dim j As Integer
Dim n As Long, r, c As Long
Dim arrRows, arrColumns As Variant
Dim Source As Range, Target As Range



For j = 4 To 18 Step 2




Set Source = Workbooks("180610_book1.xlsm").Worksheets(j).Cells
Set Target = Workbooks("180610_book2.xlsm").Worksheets("RAW DATA").Cells





arrRows = Array(5, 10, 15, 23, 28, 33, 38, 43, 48, 53, 61, 66, 71, 79, 84, 89, 94, 102, 107, 112, 117, 122, 127, 135, 140, 148, 153, 158, 166, 171, 179, 184, 189, 194)
arrColumns = Array(9, 14, 19, 24, 29, 34, 39, 44)


c = arrColumns(j - 4)

For n = 2 To 35
     r = arrRows(n - 2)
    Target.Cells(r, c).Resize(1, 5).Value = WorksheetFunction.Transpose(Source.Cells(4, n).Resize(5, 1).Value)
Next

下一个

结束子

标签: vbafor-loopcopypastenext

解决方案


我添加了一个直到只有列的计数器并且它有效

    Dim j, x As Integer
    Dim n As Long, r, c As Long
    Dim arrRows, arrColumns As Variant
    Dim Source As Range, Target As Range

    x = 0
    For j = 4 To 18 Step 2

        Set Source = Workbooks("180610_SequencingScenarioTEST1.xlsm").Worksheets(j).Cells
        Set Target = Workbooks("180610_SequencingScenarioTEST1.xlsm").Worksheets("RAW DATA").Cells
        arrRows = Array(5, 10, 15, 23, 28, 33, 38, 43, 48, 53, 61, 66, 71, 79, 84, 89, 94, 102, 107, 112, 117, 122, 127, 135, 140, 148, 153, 158, 166, 171, 179, 184, 189, 194)
        arrColumns = Array(9, 14, 19, 24, 29, 34, 39, 44)

        Do
            c = arrColumns(x)
            For n = 2 To 35

                r = arrRows(n - 2)
                Target.Cells(r, c).Resize(1, 5).Value = WorksheetFunction.Transpose(Source.Cells(4, n).Resize(5, 1).Value)
            Next                                      'for each column being transpose to row in target sheet

            x = x + 1

        Loop Until x <> x + 1                         'for each columns in raw data = the different scenarios
    Next                                              'for j=new sheet
End Sub

推荐阅读