0

我正在尝试创建一个宏以将每个工作表的最后一列(在我的情况下为 R 列)的值放入一个名为“MainSheet”的新工作表中

如果有 4 张表,则应将最后一列值提取到主表列 A、B、C 和 D

提前致谢

4

1 回答 1

1

假设您的数据总是从A1您要复制的每张工作表开始,那么这可能会让您开始:

Sub CopyLastColumns()
    Dim cnt As Integer, sht As Worksheet, mainsht As Worksheet, col As Integer, rw As Integer

    Set mainsht = Worksheets("MainSheet")

    cnt = 1
    For Each sht In Worksheets
        If sht.Name <> "MainSheet" Then
            sht.Columns(sht.Range("A1").CurrentRegion.Columns.Count).Copy
            mainsht.Columns(cnt).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
            mainsht.Cells(10, cnt) = sht.Range("A2") 
            cnt = cnt + 1
        End If
    Next sht

    With mainsht
        For col = 1 To cnt
            For rw = .Cells(65536, col).End(xlUp).Row To 1 Step -1
                If .Cells(rw, col) = "" Then
                    .Cells(rw, col).Delete Shift:=xlUp
                End If
            Next rw
        Next col
    End With

End Sub
于 2012-04-19T18:14:42.463 回答