Move row to another table when text entered into column

MinniMaz

New Member
Joined
Jul 31, 2017
Messages
5
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
I have the following code to move a row from one table to another table in the same workbook when specific text is entered in to column J. This is working fine, except it does not extend the table in the destination sheet, just adds it to the end and the existing formulas do not auto populate in the destination table. I understand this may have something to do with pasting an entire row. How do I reword this to be able to extend the table in the destination sheet?

Rich (BB code):
Sub MoveBasedOnValue()

'Created by Excel 10 Tutorial
    Dim xRg As Range
    Dim xCell As Range
    Dim A As Long
    Dim B As Long
    Dim C As Long
    A = Worksheets("PENDING").UsedRange.Rows.Count
    B = Worksheets("INVOICE").UsedRange.Rows.Count
    If B = 1 Then
       If Application.WorksheetFunction.CountA(Worksheets("INVOICE").UsedRange) = 0 Then B = 0
    End If
    Set xRg = Worksheets("PENDING").Range("J1:J" & A)
    On Error Resume Next
    Application.ScreenUpdating = False
    For C = 1 To xRg.Count
        If CStr(xRg(C).Value) = "Completed" Then
            xRg(C).EntireRow.Copy Destination:=Worksheets("INVOICE").Range("A" & B + 1)
            xRg(C).EntireRow.Delete
            If CStr(xRg(C).Value) = "Completed" Then
                C = C - 1
            End If
            B = B + 1
        End If
    Next

Thanks.
 
Last edited by a moderator:

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
VBA Code:
Sub MoveBasedOnValue()
    Dim wsSource As Worksheet
    Dim wsDest As Worksheet
    Dim xRg As ListObject
    Dim xDestRg As ListObject
    Dim xCell As Range
    Dim A As Long
    Dim B As Long

    ' Set references to the source and destination worksheets
    Set wsSource = ThisWorkbook.Sheets("PENDING")
    Set wsDest = ThisWorkbook.Sheets("INVOICE")

    ' Set references to the source and destination tables
    Set xRg = wsSource.ListObjects("Table1") ' Replace "Table1" with the actual name of your source table
    Set xDestRg = wsDest.ListObjects("Table2") ' Replace "Table2" with the actual name of your destination table

    A = xRg.ListRows.Count
    B = xDestRg.ListRows.Count

    On Error Resume Next
    Application.ScreenUpdating = False

    ' Loop through the source table
    For Each xCell In xRg.ListColumns("Status").DataBodyRange
        If xCell.Value = "Completed" Then
            ' Add a new row to the destination table
            xDestRg.ListRows.Add
            B = B + 1
            ' Copy the data (except for the formulas) from the source table to the destination table
            xRg.ListRows(xCell.Row - xRg.ListObject.HeaderRowRange.Row).Range.Copy
            xDestRg.ListRows(B).Range.PasteSpecial xlPasteValues
            ' Delete the original row in the source table
            xRg.ListRows(xCell.Row - xRg.ListObject.HeaderRowRange.Row).Delete
        End If
    Next xCell

    On Error GoTo 0
    Application.ScreenUpdating = True
End Sub

Test this one
 
Upvote 0
@MinniMaz

When posting vba code in the forum, please use the available code tags. It makes your code much easier to read/debug & copy. My signature block below has more details. I have added the tags for you this time.

I also note that you also did not act on the suggestion here.
You will get better help from the forum if you provide the information requested. ;)
 
Upvote 0
VBA Code:
Sub MoveBasedOnValue()
    Dim wsSource As Worksheet
    Dim wsDest As Worksheet
    Dim xRg As ListObject
    Dim xDestRg As ListObject
    Dim xCell As Range
    Dim A As Long
    Dim B As Long

    ' Set references to the source and destination worksheets
    Set wsSource = ThisWorkbook.Sheets("PENDING")
    Set wsDest = ThisWorkbook.Sheets("INVOICE")

    ' Set references to the source and destination tables
    Set xRg = wsSource.ListObjects("Table1") ' Replace "Table1" with the actual name of your source table
    Set xDestRg = wsDest.ListObjects("Table2") ' Replace "Table2" with the actual name of your destination table

    A = xRg.ListRows.Count
    B = xDestRg.ListRows.Count

    On Error Resume Next
    Application.ScreenUpdating = False

    ' Loop through the source table
    For Each xCell In xRg.ListColumns("Status").DataBodyRange
        If xCell.Value = "Completed" Then
            ' Add a new row to the destination table
            xDestRg.ListRows.Add
            B = B + 1
            ' Copy the data (except for the formulas) from the source table to the destination table
            xRg.ListRows(xCell.Row - xRg.ListObject.HeaderRowRange.Row).Range.Copy
            xDestRg.ListRows(B).Range.PasteSpecial xlPasteValues
            ' Delete the original row in the source table
            xRg.ListRows(xCell.Row - xRg.ListObject.HeaderRowRange.Row).Delete
        End If
    Next xCell

    On Error GoTo 0
    Application.ScreenUpdating = True
End Sub

Test this one
Thank you. However, It is doing the same thing. It is not extending the table, or the formulas within that table. When I manually type into the next row the table extends automatically, however copying and pasting with VBA does not seem to work.
 
Upvote 0
@MinniMaz

When posting vba code in the forum, please use the available code tags. It makes your code much easier to read/debug & copy. My signature block below has more details. I have added the tags for you this time.

I also note that you also did not act on the suggestion here.
You will get better help from the forum if you provide the information requested. ;)
Hi Peter,

I will amend my post, once I figure out how :) :)
 
Upvote 0
Thanks for updating your profile details.

I will amend my post, once I figure out how :) :)
If you review my previous post, you will see that I have already amended your post. ;)
 
Upvote 0

Forum statistics

Threads
1,215,086
Messages
6,123,043
Members
449,092
Latest member
ikke

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