简体   繁体   中英

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"

If there are 4 sheets the last column values should be fetched to the mainsheet columns 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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