Forum Discussion

Jarvo's avatar
Jarvo
Copper Contributor
Mar 16, 2021
Solved

VBA formula for archiving

Hi everyone,

 

I've come across a very useful VBA formula for archiving data in my tables onto another sheet. The formula is as follows:

 

Application.EnableEvents = False

If Target.Column = 4 And UCase(Target) = "COMPLETED" Then

Cells(Target.Row, Target.Column).EntireRow.Copy Destination:=Sheets("Archive").Range("A" & Rows.Count).End(xlUp).Offset(1)

End If

Application.EnableEvents = True

 

However, I can't work out how to archive data in columns A:G only, instead of the entire row. I'm new to VBA and can't work this one out. Is anyone able to help? Many thanks!

  • Jarvo 

     

    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

     

     

     

     

15 Replies

  • Jarvo 

     

    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

     

     

     

     

    • Jarvo's avatar
      Jarvo
      Copper Contributor

      This is perfect thank you! I followed an online YouTube guide so it was from there that I had placed the code into the selection change. Thank you for rectifying that! I have one more request - Do you have a code so that when the data has been copied onto the Archive sheet, the row is then deleted from the Current sheet in the same process? I hope that makes sense

      • Subodh_Tiwari_sktneer's avatar
        Subodh_Tiwari_sktneer
        Silver Contributor

        Jarvo 

        You're welcome! Glad it worked as desired.

         

        To do that, please replace the existing code with the following one...

         

        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
            Rows(Target.Row).Delete
        End If
        Skip:
        Application.CutCopyMode = 0
        Application.EnableEvents = True
        Application.ScreenUpdating = True
        End Sub
  • NikolinoDE's avatar
    NikolinoDE
    Platinum Contributor

    Jarvo 

    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

     

    • Jarvo's avatar
      Jarvo
      Copper Contributor
      I did try that but it didn't work. Thanks for your help though!
      • NikolinoDE's avatar
        NikolinoDE
        Platinum Contributor
        Would 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.

Resources