Forum Discussion
VBA formula for archiving
- Mar 17, 2021
You placed the code for Selection Change Event on Current Sheet Module. Was it intentional or you did it by mistake?
Ideally you should place the code for Change Event so that when Status gets changed in Status column of the Table on Current Sheet, the data will be copied to the Saved or Archive Sheet accordingly based on the Status selected.
Please place the following code for Change Event on Current Sheet Module. In the attached I placed the code on Current Sheet Module for you to test it.
Private Sub Worksheet_Change(ByVal Target As Range) Dim tbl As ListObject Dim rng As Range Dim dws As Worksheet Dim dlr As Long Application.ScreenUpdating = False On Error GoTo Skip Set tbl = ActiveSheet.ListObjects("Table1") If Not Intersect(Target, tbl.DataBodyRange.Columns(4)) Is Nothing Then Application.EnableEvents = False Set rng = Intersect(ActiveSheet.Rows(Target.Row), Columns("A:G")) rng.Copy Select Case Target.Value Case "Save" Set dws = ThisWorkbook.Worksheets("Saved") Case "Closed" Set dws = ThisWorkbook.Worksheets("Archive") End Select dlr = dws.Cells(Rows.Count, "D").End(xlUp).Row + 1 dws.Range("A" & dlr).PasteSpecial xlPasteAll End If Skip: Application.CutCopyMode = 0 Application.EnableEvents = True Application.ScreenUpdating = True End Sub
I'm not sure I got it right, but would you like it to be copied to columns A to G? And the inserted "A: G" in your example?
Application.EnableEvents = False
If Target.Column = 4 And UCase(Target) = "COMPLETED" Then
Cells(Target.Row, Target.Column).EntireRow.Copy Destination:=Sheets("Archive").Range("A:G" & Rows.Count).End(xlUp).Offset(1)
End If
Application.EnableEvents = True
Hope I could help you,
if this is not what you are looking for, please ignore it.
Thank you for your time and patience.
Nikolino
- NikolinoDEMar 17, 2021Platinum ContributorWould it be possible to have the whole file (without sensitive data) or code?
So I could better understand what exactly you want to achieve. Correspondingly, if I am able to offer you a better proposal for a solution.- JarvoMar 17, 2021Copper ContributorGreat yeah I've just added a copy without the data. I am also having trouble with highlighting whole columns and rows and deleting them due to this formula. If you're able to work that out too you're a star. Thanks
- NikolinoDEMar 17, 2021Platinum ContributorIf you could briefly explain what exactly you want to accomplish, it would be great.
From where to where should the data go?
Column, row, cell.
Does it necessarily have to be in VBA, should one also make a proposed solution without vba?
Thank you for your patience and understanding