宏以获取最后一列的值到新工作表

时间:2012-04-19 15:48:42

标签: excel

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

如果有4张纸,则应将最后一列值提取到主菜单栏A,B,C和C列中。 d

提前致谢

1 个答案:

答案 0 :(得分: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