+ Reply to Thread
Results 1 to 10 of 10

Copy Paste Loop ends before copy/paste is finished

Hybrid View

  1. #1
    Registered User
    Join Date
    02-20-2013
    Location
    Indianapolis, Indiana
    MS-Off Ver
    Excel 2007
    Posts
    28

    Copy Paste Loop ends before copy/paste is finished

    Hello,

    I am trying to copy the values from a pivot table into the adjacent column. My current code works fine, but it ends the loop at row 35. I cannot figure out why. The only reason the extra "Do" loop is in there is because I thought it could help... it doesn't.

    Sub Copy_and_Paste_Loop_from_Pivot_Table()
    
    Do
    
    Dim i As Variant
    
    For i = 4 To Worksheets("Detail by Line Pivot").Range("B4").Value
    
        Cells(i, 2).Select
        Selection.Copy
        Cells(i, 2).Offset(1, 1).Select
          
        Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, skipblanks:=False, Transpose:=False
        
    Next i
    
    Loop Until IsEmpty(ActiveCell.Offset(0, 1))
    
    End Sub
    Thank you!

  2. #2
    Valued Forum Contributor
    Join Date
    08-13-2012
    Location
    Gardony, Hungary
    MS-Off Ver
    Excel 2003
    Posts
    558

    Re: Copy Paste Loop ends before copy/paste is finished

    Hi brgr4u.

    Probaby Worksheets("Detail by Line Pivot").Range("B4").Value
    is 35?

  3. #3
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: Copy Paste Loop ends before copy/paste is finished

    Try this
    Sub Copy_and_Paste_Loop_from_Pivot_Table()
    Dim i As Variant
    
    With Worksheets("Detail by Line Pivot")
        For i = 4 To .Range("B4").Value
            .Cells(i, 2).Copy
            .Cells(i + 1, 3).PasteSpecial Paste:=xlPasteFormulas
        Next i
    End With
    
    End Sub
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

  4. #4
    Registered User
    Join Date
    02-20-2013
    Location
    Indianapolis, Indiana
    MS-Off Ver
    Excel 2007
    Posts
    28

    Re: Copy Paste Loop ends before copy/paste is finished

    It is still ending the loop at row 35. Additionally, the cells that need to be copied in rows 35 and 36 both have a value of 35.

  5. #5
    Registered User
    Join Date
    02-20-2013
    Location
    Indianapolis, Indiana
    MS-Off Ver
    Excel 2007
    Posts
    28

    Re: Copy Paste Loop ends before copy/paste is finished

    You guessed it. Do you know of any way to work around this?

  6. #6
    Valued Forum Contributor
    Join Date
    08-13-2012
    Location
    Gardony, Hungary
    MS-Off Ver
    Excel 2003
    Posts
    558

    Re: Copy Paste Loop ends before copy/paste is finished

    Not sure if this is what you want, but here's my try.
    Sub CopyRange()
    
    Dim RngToCopy As Range 'range to copy
    Dim Lr As Long 'last row
    
    With Worksheets("Detail by Line Pivot")
        Lr = .Range("B" & Rows.Count).End(xlUp).Row
        Set RngToCopy = .Range("B4:B" & Lr)
        RngToCopy.Copy
        RngToCopy.Offset(1, 1).PasteSpecial xlPasteFormulas
    End With
    
    End Sub
    oops. Edited to paste special.
    Last edited by RHCPgergo; 04-08-2013 at 03:18 PM.

  7. #7
    Registered User
    Join Date
    02-20-2013
    Location
    Indianapolis, Indiana
    MS-Off Ver
    Excel 2007
    Posts
    28

    Re: Copy Paste Loop ends before copy/paste is finished

    Quote Originally Posted by RHCPgergo View Post
    Not sure if this is what you want, but here's my try.
    Sub CopyRange()
    
    Dim RngToCopy As Range 'range to copy
    Dim Lr As Long 'last row
    
    With Worksheets("Detail by Line Pivot")
        Lr = .Range("B" & Rows.Count).End(xlUp).Row
        Set RngToCopy = .Range("B4:B" & Lr)
        RngToCopy.Copy
        RngToCopy.Offset(1, 1).PasteSpecial xlPasteFormulas
    End With
    
    End Sub
    oops. Edited to paste special.
    Yes! That is 95% of what I want, I can take the rest from here. Thank you!!

  8. #8
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: Copy Paste Loop ends before copy/paste is finished

    Your code will stop at the row matching the value in cell B4. What value do you have there?

  9. #9
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: Copy Paste Loop ends before copy/paste is finished

    Based on your last post in this thread, its apparent that you are satisfied with the solution(s) you've received and have solved your question, but you haven't marked your thread as "SOLVED". I will do it for you this time.

    In future, to mark your thread as Solved, you can do the following -
    Select Thread Tools-> Mark thread as Solved.

    Incase your issue is not solved, you can undo it as follows -
    Select Thread Tools-> Mark thread as Unsolved.

    Also, since you are relatively new to the forum, i would like to inform you that you can thank those who have helped you by clicking the small star icon located in the lower left corner of the post which helped you. This adds to the reputation of the person who has taken the time to help you.

  10. #10
    Registered User
    Join Date
    02-20-2013
    Location
    Indianapolis, Indiana
    MS-Off Ver
    Excel 2007
    Posts
    28

    Re: Copy Paste Loop ends before copy/paste is finished

    Thank you arlu1201!

+ 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