Search code examples
excelvba

Moving cell data from a table row to another table row in the same sheet


I've recently started using macros in excel and I want to create a macro that can transfer a whole table row (not the whole excel sheet row but a row from a specific table) to another table within the same sheet, based on what row the user has selected. After it has transferred the data from the cells I need it to delete the active row that has been selected and add another row to the new table, that I'm transferring the data to. I've done some self-learning and compiled a code, but when I try to execute it, it runs into a problem with the deletion of the existing table row.

Here's the code that I've compiled:

Sub Task_Done()
'
' Task_Done Macro
'
Dim rng As Range
    
    On Error Resume Next
    With Selection.Cells(1)
        Set rng = Intersect(.EntireRow, ActiveCell.ListObject.DataBodyRange)
        On Error GoTo 0
        If rng Is Nothing Then
            MsgBox "Please select a valid table cell.", vbCritical
        Else
            rng.Cut
     
    Range("Table6[[#Headers],[Çàäà÷à]]").Select
    Selection.End(xlDown).Select
    ActiveCell.Offset(1, 0).Range("A1").Select
    ActiveSheet.Paste

    Range("Table2[[#Headers],[Çàäà÷à]]").Select
    Selection.End(xlDown).Select
    ActiveCell.Offset(1, 0).Range("A1").Select
    Selection.ListObject.ListRows(3).Delete
    
       End If
    End With
    
End Sub

Thank you to anyone who can come to aid on this :).


Solution

  • Maybe this way:

    Sub Task_Done()
    '
    ' Task_Done Macro
    '
    Dim rng As Range
        
        On Error Resume Next
        Set rng = Intersect(Selection.Cells(1).EntireRow, ActiveCell.ListObject.DataBodyRange)
        On Error GoTo 0
        If rng Is Nothing Then
            MsgBox "Please select a valid table cell.", vbCritical
        Else
            rng.Cut
         
            Range("Table6[#Headers]").End(xlDown).Offset(1, 0).Select
            ActiveSheet.Paste
    
            Range("Table2[#Headers]").End(xlDown).Offset(1, 0).Select
            Selection.ListObject.ListRows(Selection.Row - Range("Table2[#Headers]").Row).Delete
        
        End If
        
    End Sub