将多张纸中的数据复制到一张纸中

时间:2020-03-29 07:11:03

标签: excel vba

我正在尝试将特定的单元(I106到I160)从多张纸复制到第一张纸。每一个都应该在新列中彼此相邻地复制。但是,它将数据复制到彼此下面的1列中。

预先感谢

Sub CopyIt()

    Dim ws As Worksheet
    Application.ScreenUpdating = False
    For Each ws In ActiveWorkbook.Worksheets
        If ws.name <> "All Data" Then
            ws.Range("I106:I160").Copy
            Sheets("All Data").Cells(Rows.Count, "B").End(xlUp).Offset(1).PasteSpecial xlPasteValues
        End If
    Next
    Application.ScreenUpdating = True
End Sub

1 个答案:

答案 0 :(得分:3)

尝试一下:

Sub CopyIt()

    Dim ws As Worksheet, c As Range, wsData As Worksheet, wb As WorkBook
    Set wb = ActiveWorkbook
    Set wsData = wb.Sheets("All Data")
    Application.ScreenUpdating = False
    Set c = wsData.Cells(Rows.Count, "B").End(xlUp).Offset(1) 'start here
    For Each ws In wb.Worksheets
        If ws.name <> wsData.Name Then
            With ws.Range("I106:I160")
                c.Resize(.Rows.Count, .Columns.Count).Value = .Value
            End With
            Set c = c.offset(0, 1)'next column 
        End If
    Next
    Application.ScreenUpdating = True
End Sub