Attempting to Archive data in a worksheet

JDoc27

Board Regular
Joined
Aug 1, 2012
Messages
60
Hi Guys,

I am currently trying to archive data from a worksheet that is in continual use.

What I would like to happen is that when an action is listed as Complete then that row from the worksheet is moved into the next available row of a new one, this is the current VBA I am attempting to use but I keep being met with the error "Run-time error '1004': Application-defined or object-defined error".
Sub Archive()
Dim r As Long, endRow As Long, pasteRowIndex As Long


endRow = 10 '
pasteRowIndex = 1


For r = 1 To endRow


If Cells(r, Columns("E").Column).Value = "Complete" Then

Rows(r).Select
Selection.Copy


Sheets("Archive").Select
Rows(pasteRowIndex).Select
ActiveSheet.Paste


pasteRowIndex = pasteRowIndex + 1




Sheets("Queries").Select
End If
Next r
End Sub

I should also mention that the description Complete is one criterion of a drop down menu.

I would be grateful if somebody could please identify where I am going wrong with this.

Thanks
 
Last edited:

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Copy and paste this macro into the worksheet code module. Do the following: right click the tab for your "Queries" sheet and click 'View Code'. Paste the macro into the empty code window that opens up. Close the code window to return to your sheet. The macro will be run automatically when you choose "Complete" in column E.
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Application.ScreenUpdating = False
    If Intersect(Target, Range("E:E")) Is Nothing Then Exit Sub
    If Target = "Complete" Then
        Target.EntireRow.Copy Sheets("Archive").Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)
    End If
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Thanks for the help Mumps.

Every time a row is changed to Complete it is overwriting the previous row that has been copied over. Can you help with this?

Also I would like to have it delete from the originating table once it has been copied.

Many Thanks
 
Upvote 0
Try:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Intersect(Target, Range("E:E")) Is Nothing Then Exit Sub
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    On Error GoTo errHandler
    If Target = "Complete" Then
        Target.EntireRow.Copy Sheets("Archive").Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)
        Target.EntireRow.Delete
    End If
errHandler:
    Application.EnableEvents = True
    Application.ScreenUpdating = True
End Sub
When I tested the code on some dummy data, it worked properly without overwriting the previous row. If it still doesn't work properly, perhaps you could upload a copy of your file to a free site such as www.box.com. Once you do that, mark it for 'Sharing' and you will be given a link to the file that you can post here. It will be easier to check out the problem if I can see your file.
 
Upvote 0

Forum statistics

Threads
1,214,523
Messages
6,120,031
Members
448,940
Latest member
mdusw

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