+ Reply to Thread
Results 1 to 3 of 3

Paste to next open column

Hybrid View

  1. #1
    Registered User
    Join Date
    09-03-2012
    Location
    Virginia
    MS-Off Ver
    excel 2010
    Posts
    18

    Paste to next open column

    Hi

    I have data in multiple sheets and would like to copy from selected cells (last 252 rows in Column G) of each sheet and paste to next open column. I also need the sheet name pasted into the top of each column. This is what I have so far, but it is not working properly. I can't figure out the paste part of the macro.

    Sub Copylast252rows()
    
    Dim wkst As Worksheet
    Dim row As Long 
    Dim LastRow As Long
    Dim Col As Integer
    Dim shtname As String
    Application.ScreenUpdating = False
    For Each wkst In ActiveWorkbook.Worksheets
    ' loop through the Open worksheets
       If wkst.Name <> "Summary"  Then
          wkst.Select
          shtname = wkst.Name
          LastRow = Range("G" & Rows.Count).End(xlUp).row
          Range("G" & LastRow - 252 & ":G" & LastRow).Copy
          
          Sheets("Summary").Select
          Col = ActiveSheet.UsedRange.Columns.Count
          Cells(1, Col).Value = shtname
          Cells(2, Col).Select
          Selection.Paste
            
       End If
    Next
    
    End Sub

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,646

    Re: Paste to next open column

    Try this.
    Sub Copylast252rows()
    Dim wkst As Worksheet
    Dim rngDst As Range
    Dim rngSrc As Range
    
        Set rngDst = Worksheets("Summary").Cells(1, Columns.Count).End(xlToLeft).Offset(, 1)
    
        Application.ScreenUpdating = False
    
        For Each wkst In ActiveWorkbook.Worksheets
            ' loop through the Open worksheets
            If wkst.Name <> "Summary" Then
    
                With wkst
                    Set rngSrc = .Range("G" & Rows.Count).End(xlUp)
                    Set rngSrc = .Range(rngSrc.Offset(-251), rngSrc)
                End With
    
                rngDst.Value = wkst.Name
    
                rngSrc.Copy rngDst.Offset(1)
    
                Set rngDst = rngDst.Offset(, 1)
            End If
        Next
    
    End Sub
    If posting code please use code tags, see here.

  3. #3
    Registered User
    Join Date
    09-03-2012
    Location
    Virginia
    MS-Off Ver
    excel 2010
    Posts
    18

    Re: Paste to next open column

    Wow, Norie, that is perfect. Thank you so much.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1