+ Reply to Thread
Results 1 to 10 of 10

EntireRow copy/paste won't stop

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    05-23-2007
    Posts
    126

    EntireRow copy/paste won't stop

    The code below 'works' but doesn't stop.

    It correctly takes the first x number of rows with "y" in column F of sheet a and pastes them one after another on sheet b. Instead of stopping after the last "y", however, it then takes the first "y" row occurrence of sheet a and fills that in every row to the very end of sheet b (row 1,048,576). So sheet b does have what I need, then about 1,048,500 more rows that I don't need....

    Thanks in advance for any help.

        Dim cel As Range
        Worksheets("sheet a").Activate
        For Each cel In Range("f2:f1000")
            If cel.Value = "y" Then
            cel.EntireRow.Copy
            Worksheets("sheet b").Activate
            ActiveSheet.Paste
            Application.CutCopyMode = False
            ActiveCell.Offset(1, 0).Select
            End If
        Next

  2. #2
    Valued Forum Contributor
    Join Date
    11-04-2018
    Location
    Denpasar
    MS-Off Ver
    Excel 2010
    Posts
    777

    Re: EntireRow copy/paste won't stop

    because the active sheet now is not sheet a anymore
    maybe try like this:

    Dim cel As Range
        Worksheets("sheet a").Activate
        For Each cel In Range("f2:f1000")
            If cel.Value = "y" Then
            cel.EntireRow.Copy
            Worksheets("sheet b").Activate
            ActiveSheet.Paste
            Application.CutCopyMode = False
            ActiveCell.Offset(1, 0).Select
            End If
           Worksheets("sheet a").Activate
        Next
    FYI, it will show you sheet a and sheet b back n forth.

    If you know what row to be pasted in sheet b, (the last blank row, for example)
    the code can be modified.
    Last edited by karmapala; 09-23-2021 at 02:00 PM.

  3. #3
    Forum Contributor
    Join Date
    05-23-2007
    Posts
    126

    Re: EntireRow copy/paste won't stop

    Odd... I've used this little bit of code for years (below), and only revised it slightly for the new file. Never had this happen before... was always switching between 2 sheets without issue.
    Any ideas what I could look for that might be causing the new issue, if it isn't the sheet switching?

        Dim cel As Range
        Worksheets("sheet a").Activate
        For Each cel In Range("A4:A150")
            If cel.Value = "y" Then
            cel.EntireRow.Copy
            Worksheets("sheet b").Activate
            ActiveSheet.Paste
            Application.CutCopyMode = False
            ActiveCell.Offset(1, 0).Select
            End If
        Next

  4. #4
    Valued Forum Contributor
    Join Date
    11-04-2018
    Location
    Denpasar
    MS-Off Ver
    Excel 2010
    Posts
    777

    Re: EntireRow copy/paste won't stop

    Try to put line :
    cel.select
    right below this line
    For Each cel In Range("A4:A150")
    Do debug F8,
    and see what happen when actually it should find the "Y" for the second time in the looped cel.

  5. #5
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,913

    Re: EntireRow copy/paste won't stop

    There are much better ways to do this but this will get you started.

    Instead of copying entire rows just change 4 in Resize (both times) to the number of columns that need to be copied.

    Sub tst()
        With Sheets("sheet a")
            For Each cl In .Range("A4", .Range("A" & .Rows.Count).End(xlUp))
                If cl.Value = "y" Then
                    Sheets("sheet b").Range("A" & .Rows.Count).End(xlUp).Offset(1).Resize(, 4).Value = cl.Resize(, 4).Value
                End If
            Next
        End With
    End Sub
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

  6. #6
    Forum Contributor
    Join Date
    05-23-2007
    Posts
    126

    Re: EntireRow copy/paste won't stop

    Quote Originally Posted by bakerman2 View Post
    Instead of copying entire rows just change 4 in Resize (both times) to the number of columns that need to be copied.
    This works, but how can I revise such that it captures columns also that precede the "y" column? "Y" column is column F, and would need to copy A through P.

    Thanks

  7. #7
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,913

    Re: EntireRow copy/paste won't stop

    Sub tst()
        With Sheets("sheet a")
            For Each cl In .Range("F4", .Range("F" & .Rows.Count).End(xlUp))
                If cl.Value = "y" Then
                    Sheets("sheet b").Range("A" & Rows.Count).End(xlUp).Offset(1).Resize(, 16).Value = .Range("A" & cl.Row).Resize(, 16).Value
                End If
            Next
        End With
    End Sub
    If you were to post an example workbook then we could propose better ways to do this.(like Autofilter or AdvancedFilter)

  8. #8
    Forum Contributor
    Join Date
    05-23-2007
    Posts
    126

    Re: EntireRow copy/paste won't stop

    Quote Originally Posted by bakerman2 View Post
    If you were to post an example workbook then we could propose better ways to do this.(like Autofilter or AdvancedFilter)
    My apologies, thought I had on this one... I'll try this latest and will upload sample file if I'm still having issues.
    Thank you very much for your help!

  9. #9
    Forum Contributor
    Join Date
    05-23-2007
    Posts
    126

    Re: EntireRow copy/paste won't stop

    That did indeed fix it bakerman! Thank you!

  10. #10
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,913

    Re: EntireRow copy/paste won't stop

    You're welcome and thanks for rep+.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Code to stop cut copy paste and other keys
    By nr6281 in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 08-17-2021, 06:40 PM
  2. [SOLVED] Copy entirerow from a Workbook & paste into another
    By noboffinme in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-29-2019, 09:37 PM
  3. VBA to copy/paste down values and stop at row with text
    By lukestkd in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-26-2015, 07:11 AM
  4. [SOLVED] Cut, Copy or Paste causes excel to stop responding.
    By Jimizx in forum Excel General
    Replies: 2
    Last Post: 10-19-2012, 09:43 PM
  5. copy/paste entirerow based on listitems
    By GregJG in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-28-2008, 08:47 AM
  6. Stop users from Cut/Copy & Paste
    By rmcveigh in forum Excel General
    Replies: 2
    Last Post: 11-21-2006, 04:09 PM
  7. How to stop formulas from incrementing when you copy and paste?
    By Kirkwill in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 01-28-2005, 02:06 PM

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