【问题标题】:Copy specific columns from multiple sheets into one将多个工作表中的特定列复制到一个工作表中
【发布时间】:2016-04-24 14:29:13
【问题描述】:

我是 VBA 新手。我一直在寻找几个小时,但无济于事:(

我有 12 张由 A-T 列组成的工作表。我想通过使用宏将 12 张工作表中的 C 和 T 列复制并合并到一个工作簿“摘要”中。有人能帮我吗?提前致谢。

Sub Create_Summary()

Application.DisplayAlerts = False
On Error Resume Next

Application.DisplayAlerts = True
 n = Application.Worksheets.Count

Sheets("Summary").Move after:=Worksheets(Worksheets.Count)

Dim sh As Worksheet
For Each sh In ActiveWorkbook.Worksheets
   If sh.Name <> "Summary" Then
    Set col = Columns(Columns.Count).End(xlToLeft)
      Set col = Columns(Columns.Count).End(xlToLeft)
        sh.Range("C:C,T:T").Copy Destination:=Sheets("Summary").Range(col,col)
    End If

Next sh

End Sub

这从最后一张表复制 C 和 T 列,而其他只是 C 没有 T。

【问题讨论】:

  • 您尝试录制宏吗?
  • 请告诉我们你到目前为止尝试了什么。
  • 我已经添加了代码,希望你能帮忙。

标签: vba excel


【解决方案1】:

试试这个

Option Explicit

Sub Create_Summary()
Dim sh As Worksheet, sumSht As Worksheet
Dim i As Long

Set sumSht = Sheets("Summary")
sumSht.Move after:=Worksheets(Worksheets.Count)

For i = 1 To Worksheets.Count - 1 ' once you moved "Summary" sheet as the workbook last one, you skip it by limiting loop to the penultimate sheets index
    Worksheets(i).Range("C:C,T:T").Copy Destination:=sumSht.Cells(1, sumSht.Columns.Count).End(xlToLeft).Offset(, 1) ' qualify all destination references to "Summary" sheet
Next i
sumSht.Columns(1).Delete ' "Summary" sheet first column gest skipped by the above loop, so delete it

End Sub

已注释,以便您可以关注并进行更改

【讨论】:

  • 不客气。如果我完成了您的问题,请将答案标记为已接受。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-27
  • 1970-01-01
  • 2015-05-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多