vba - 将多张工作表中的特定列复制到一张中

标签 vba excel

我是 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 列。

最佳答案

试试这个

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

它已被注释,以便您可以遵循它并进行更改

关于vba - 将多张工作表中的特定列复制到一张中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36824404/

相关文章:

sql - 在值不为空的地方插入

VBA 调试——查看所有打开的 DAO 记录集

vba - Excel VBA : Copying across unique rows from one sheet to another

VBA - 冒号 `:` 如何在带有条件的 VBA 代码中工作

ms-access - 如何计算表中的字段数?

excel - 当我引用未命名的范围时出现运行时错误 '1004'

c# - 在 C# 中将 Excel 列(或单元格)格式化为文本?

excel - 单独列中的日期

.net - Jet Engine - 255 个字符截断

excel - 使用 if 和 array 从 Excel 中的未排序列表创建排序列表