gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 20:12:05 25 4
gpt4 key购买 nike

我是 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com