+ Reply to Thread
Results 1 to 5 of 5

Macro to copy & paste, but not delete

  1. #1
    Registered User
    Join Date
    10-06-2017
    Location
    Leesburg, Virginia
    MS-Off Ver
    2016
    Posts
    4

    Macro to copy & paste, but not delete

    Hello all,

    I found a macro that will copy and paste rows to a new sheet based on a certain condition, and then delete the rows from the parent sheet. However, I'd like to copy and paste *without* then deleting the rows.

    I tried removing the delete function from the code, but it hangs up Excel and doesn't work.

    Anyone have any suggestions?

    Thanks,
    Leif



    Here's the code:


    Sub resolution()

    Dim xRg As Range
    Dim xCell As Range
    Dim I As Long
    Dim J As Long
    Dim K As Long


    ' Keyetta

    ' exchange "Keyetta" in row below with new sheet name
    I = Worksheets("Keyetta").UsedRange.Rows.Count
    J = Worksheets("Summary").UsedRange.Rows.Count
    If J = 1 Then
    If Application.WorksheetFunction.CountA(Worksheets("Summary").UsedRange) = 0 Then J = 0
    End If

    ' exchange "Keyetta" in row below with new sheet name
    Set xRg = Worksheets("Keyetta").Range("K1:K" & I)
    On Error Resume Next
    Application.ScreenUpdating = False
    For K = 1 To xRg.Count
    If CStr(xRg(K).Value) = "NO" Then
    xRg(K).EntireRow.Copy Destination:=Worksheets("Summary").Range("A" & J + 1)
    xRg(K).EntireRow.Delete
    If CStr(xRg(K).Value) = "NO" Then
    K = K - 1
    End If
    J = J + 1
    End If
    Next
    Application.ScreenUpdating = True

    ' End code for Keyetta



    End Sub

  2. #2
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,482

    Re: Macro to copy & paste, but not delete

    The line that deletes the row is this one:
    Please Login or Register  to view this content.
    Delete the line should stop the row from getting deleted

    Edit: HA, I just read what you said about deleting the line, I will copy the code and test it out.

  3. #3
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,482

    Re: Macro to copy & paste, but not delete

    try this,

    Please Login or Register  to view this content.

  4. #4
    Registered User
    Join Date
    10-06-2017
    Location
    Leesburg, Virginia
    MS-Off Ver
    2016
    Posts
    4

    Re: Macro to copy & paste, but not delete

    Thanks, davesexcel - this works for copying over, but if it's run more than once (perhaps by accidentally initiating it), it'll copy the same rows at the bottom.

    Is there a way to prevent duplicates?

  5. #5
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,482

    Re: Macro to copy & paste, but not delete

    I'm not sure, is there a way to determine if there are duplicates?

    BTW, the reason your first code was freezing was that k=1 all the time
    `If CStr(xRg(K).Value) = "NO" Then`
    since the row never got deleted, it would always ="NO"

+ 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. Copy, transpose paste, and delete rows macro
    By johnjoepaddyfrancie in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-24-2017, 08:58 AM
  2. Macro to copy, paste then delete depending on a certain word in the row
    By melliard5779259 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 01-02-2014, 05:36 AM
  3. [SOLVED] Need Macro to select and copy data, paste it to a new location, then delete the old row
    By summerela in forum Excel Programming / VBA / Macros
    Replies: 20
    Last Post: 09-25-2012, 05:22 PM
  4. Macro to copy/paste info in different sheets and to delete last entry
    By barcas84 in forum Excel Programming / VBA / Macros
    Replies: 25
    Last Post: 02-02-2012, 02:30 PM
  5. Copy and paste/ delete ranges from mulitpuls sheets macro
    By girt0n in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-07-2012, 08:23 AM
  6. macro to copy -paste-delete
    By koklok in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-10-2009, 03:54 PM
  7. [SOLVED] Macro to copy, paste in a range and then delete
    By Garry in forum Excel General
    Replies: 0
    Last Post: 03-23-2006, 03:40 PM

Tags for this Thread

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