简体   繁体   English

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

[英]Macro to get values of last column to new sheet

I am trying to create a Macro to get the values of the last column (in my case column R) of each sheet into a new sheet named "MainSheet" 我正在尝试创建一个宏,以将每个工作表的最后一列(在我的情况下为R列)的值转换为名为“ MainSheet”的新工作表

If there are 4 sheets the last column values should be fetched to the mainsheet columns A, B, C & D 如果有4张纸,则最后一列的值应提取到主纸的A,B,C和D列

Thanks in advance 提前致谢

Assuming your data always starts in A1 on each of the sheets that you want to copy from then this may get you started: 假设您的数据总是从要复制的每张纸的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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM