Copy, paste and delete rows in vba

postleneo

New Member
Joined
May 6, 2010
Messages
6
Hi guys I'm hoping if anyone could provide me assistance. I have a table in worksheet 1 with data in rows 3 to 1000 and columns A to P. If column P holds a date for any row I need to copy the full row, paste the row to the next available row in worksheet 2 and then delete the appropriate rows from worksheet 1. I have a basic understanding of vba and any help to achieve this would be appreciated
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Try:


Code:
Sub Copy_delete()
    'Copy, paste and delete rows in vba
    Dim uf As Long, i As Long, datos As Range, r As Range, fech As Range
    Dim h1 As Worksheet, h2 As Worksheet
    '
    Application.ScreenUpdating = False
    Set h1 = Sheets("sheet1")
    Set h2 = Sheets("sheet2")
    '
    uf = h1.Range("P" & Rows.Count).End(xlUp).Row
    Set datos = h1.Range("P3:P" & uf)
    For Each fech In datos
        If IsDate(fech.Value) Then
            If r Is Nothing Then
                Set r = h1.Range("P" & fech.Row)
            Else
                Set r = Union(r, h1.Range("P" & fech.Row))
            End If
        End If
    Next
    If Not r Is Nothing Then
        uf = h2.Range("P" & Rows.Count).End(xlUp).Row + 1
        r.EntireRow.Copy h2.Range("A" & uf)
        r.EntireRow.Delete
        Set r = Nothing
    End If
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
I'm glad to help you. Thanks for the feedback.
 
Upvote 0
Just another issue.... If there is no date in column P but a value N/K.... How would I change the code to copy paste and delete the rows for either entry?
 
Upvote 0
Change this:

Code:
[COLOR=#333333]If IsDate(fech.Value) Then[/COLOR]

By:

Code:
[COLOR=#333333]If fech.Value <> "" Then[/COLOR]
 
Upvote 0

Forum statistics

Threads
1,215,036
Messages
6,122,794
Members
449,095
Latest member
m_smith_solihull

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top