将特定的列从多张纸复制到一张纸

时间:2018-12-23 05:54:06

标签: excel vba

请帮助我解决这个问题。

我想将“ B”中的所有列复制到工作表的每个表列的末尾,并将其复制到名为“ combined”的新工作表中。 “合并”工作表中的页眉表格与每张工作表(“ A”)相同

代码:

Sub Combine()
   
   ' Sheets(1).Select
   ' Worksheets.Add ' add a sheet in first place
    Sheets(1).Name = "Combined"

    ' copy headings
    Sheets(2).Activate
    Range("A1").EntireColumn.Select
    Selection.Copy Destination:=Sheets(1).Range("A1")

    Dim ws As Worksheet
    Dim wsDest As Worksheet

    Set wsDest = Sheets("Combined")
    
      For Each ws In ActiveWorkbook.Sheets
            If ws.Name <> wsDest.Name Then
                ws.Range("B1", ws.Range("B1").End(xlToRight).End(xlDown)).Copy
                wsDest.Cells(1, Columns.Count).End(xlToLeft).Offset("B").PasteSpecial xlPasteValues
            End If
        
       Next ws
    
End Sub

感谢高级...

1 个答案:

答案 0 :(得分:0)

.Offset("B")无效的语法

将一列向右移.Offset(, 1)

Dim ws As Worksheet
Dim wsDest As Worksheet

Set wsDest = Sheets("Combined")

For Each ws In ActiveWorkbook.Sheets
    If ws.Name <> wsDest.Name Then
        ws.Range("B1", ws.Range("B1").End(xlToRight).End(xlDown)).Copy
        wsDest.Cells(1, Columns.Count).End(xlToLeft).Offset(, 1).PasteSpecial xlPasteValues
    End If
Next ws