Forum Discussion

ChicagoLane's avatar
ChicagoLane
Copper Contributor
Jan 11, 2024

VBA Needed - Move Row From Table to Different Table based on Cell Value

Hello...  I have a table with a column B called "Owned By".  I will create a Data Validation List with 8 names to select from in that column. 

I would like my team of 8 people to be able to select their name in column B and click a button to move the row to their table and delete the row from the source table.

I did try the following VBA to move a row from the source table to another, then delete the row from the source table by adding X in Column B:

 

Sub MoveCRLIJ()
Dim fCell As Range
Dim wsSearch As Worksheet
Dim wsDest As Worksheet
Dim lastRow As Long

Set wsSearch = Worksheets("CR-LIJ")
'Where should we move the data?
Set wsDest = Worksheets("Worksheet")

Application.ScreenUpdating = False

With wsSearch.Range("B:B")
Set fCell = .Find(what:="X", LookIn:=xlValues, lookat:=xlWhole, MatchCase:=False)

lastRow = wsDest.Cells(.Rows.Count, "A").End(xlUp).Offset(1).Row

wsSearch.Cells(fCell.Row, "A").Resize(1, 38).Copy
wsDest.Cells(lastRow, "A").PasteSpecial Paste:=xlPasteValues

fCell.EntireRow.Delete

'Try to find next one
Set fCell = .Find(what:="x", LookIn:=xlValues, lookat:=xlWhole, MatchCase:=False)
Loop


If lastRow <> 0 Then
wsDest.ListObjects("CRLIJWork").Resize wsDest.Range("A2:AR" & lastRow)
End If
End With

'Reset
Application.ScreenUpdating = True

End Sub

 

Now I need to know how to change this so the 8 people using this can move to their own table based on there name selected in column B.  Whatcha think??

  • I just received this code from another forum that worked perfectly:

    Sub MoveCRLIJ()
         Dim V, R&, W
       With Sheet2.ListObjects(1)
            V = Filter(.Parent.Evaluate(Replace("TRANSPOSE(IF(#>0,ROW(#)-" & .Range.Row & "))", "#", .DataBodyRange.Columns(2).Address)), False, False)
        For R = 0 To UBound(V)
         If Evaluate("ISREF('" & .ListRows(V(R)).Range(2) & "'!A1)") Then
            W = .ListRows(V(R)).Range
       With Sheets(.ListRows(V(R)).Range(2).Text).Cells(Rows.Count, 1).End(xlUp)
           .Cells(2 + (.Text = "")).Resize(, UBound(W, 2)) = W
       End With
         Else
            V(R) = False
         End If
        Next
            V = Filter(V, False, False)
            For R = UBound(V) To 0 Step -1:  .ListRows(V(R)).Delete:  Next
       End With
    End Sub
  • NikolinoDE's avatar
    NikolinoDE
    Gold Contributor

    ChicagoLane 

    To modify the VBA code to move rows to individual tables based on the name selected in column B, you can make the following changes. I'll assume that each person has a separate worksheet and that the worksheet names match the names in the "Owned By" list:

    Vba Code is untested, please backup your file before you use it.

    Sub MoveToIndividualTable()
        Dim wsSearch As Worksheet
        Dim wsDest As Worksheet
        Dim lastRow As Long
        Dim ownerName As String
        Dim destTableName As String
    
        Set wsSearch = Worksheets("CR-LIJ")
        Set wsDest = Nothing ' Initialize the destination worksheet
    
        ' Turn off alerts to avoid prompts when deleting rows
        Application.DisplayAlerts = False
    
        ' Loop through each row in column B
        For Each cell In wsSearch.Range("B2:B" & wsSearch.Cells(wsSearch.Rows.Count, "B").End(xlUp).Row)
            If cell.Value <> "" Then ' Check if the cell is not empty
                ownerName = cell.Value
                destTableName = "Table" & ownerName ' Adjust the table names as needed
    
                ' Check if the destination worksheet exists
                On Error Resume Next
                Set wsDest = Worksheets(destTableName)
                On Error GoTo 0
    
                If wsDest Is Nothing Then ' If the destination worksheet doesn't exist, create it
                    Set wsDest = Sheets.Add(After:=Sheets(Sheets.Count))
                    wsDest.Name = destTableName
                    ' Add code here to set up the structure of the new worksheet, e.g., headers, formatting, etc.
                End If
    
                ' Find the last row in the destination worksheet
                lastRow = wsDest.Cells(wsDest.Rows.Count, "A").End(xlUp).Offset(1).Row
    
                ' Copy the row to the destination worksheet
                wsSearch.Cells(cell.Row, "A").Resize(1, 38).Copy
                wsDest.Cells(lastRow, "A").PasteSpecial Paste:=xlPasteValues
    
                ' Clear the row in the source worksheet
                wsSearch.Rows(cell.Row).Clear
    
                ' If needed, add code here to update the destination table structure
            End If
        Next cell
    
        ' Reset alerts
        Application.DisplayAlerts = True
    End Sub

    This modified code assumes that each person's worksheet is named "Table" followed by their name in the "Owned By" list. For example, if a person's name is "John," their worksheet should be named "TableJohn."

    Before using this code, make sure to adjust the destination table names and consider any specific setup required for each person's worksheet. Additionally, you may need to modify the code to match the actual structure of your tables and worksheets. The text, steps and functions were created with the help of AI.

     

    My answers are voluntary and without guarantee!

     

    Hope this will help you.

    Was the answer useful? Mark as best response and Like it!

    This will help all forum participants.

    • ChicagoLane's avatar
      ChicagoLane
      Copper Contributor

      NikolinoDE 

      Thank you for the response...  I am afraid I am a VBA newbie and found the original code I posted in another forum so I am struggling to add the missing code you noted to change the structure of the destination table, etc.  When testing your code, the row moved to a new worksheet as plain text and deleted from the source but left a blank row in the source table.  I tried to logic my way through picking pieces out of your code to update the original code and got an error.  Here is where I left off:

      Sub MoveCRLIJ()
          Dim fCell As Range
          Dim wsSearch As Worksheet
          Dim wsDest As Worksheet
          Dim lastRow As Long
          Dim ownerName As String
          Dim destTableName As String
          
          'What sheet are we searching?
          Set wsSearch = Worksheets("CR-LIJ")
          'Where should we move the data?
          Set wsDest = Nothing
          
          'Prevent screen flicker
          Application.ScreenUpdating = False
          
          'We will be searching col B
          With wsSearch.Range("B:B")
              'Find the word "X"
              Set fCell = .Find(what:=ownerName, LookIn:=xlValues, lookat:=xlWhole, MatchCase:=False)
              If cell.Value <> "" Then ' Check if the cell is not empty
                  ownerName = cell.Value
                  destTableName = ownerName ' Adjust the table names as needed
              
               ' Check if the destination worksheet exists
                  On Error Resume Next
                  Set wsDest = Worksheets(destTableName)
                  On Error GoTo 0
          
              'Repeat until we've moved all the records
              Do Until fCell Is Nothing
                  'Found something, copy and delete
                  'Where will we paste to?
                  lastRow = wsDest.Cells(.Rows.Count, "A").End(xlUp).Offset(1).Row
                  
                  'Copy A:AL
                  wsSearch.Cells(fCell.Row, "A").Resize(1, 38).Copy
                  wsDest.Cells(lastRow, "A").PasteSpecial Paste:=xlPasteValues
           
                  fCell.EntireRow.Delete
                  
                  'Try to find next one
                  Set fCell = .Find(what:=ownerName, LookIn:=xlValues, lookat:=xlWhole, MatchCase:=False)
              Loop
              
              'Resize our table to match new data
              If lastRow <> 0 Then
                  wsDest.ListObjects("CRLIJWork").Resize wsDest.Range("A2:AS" & lastRow)
               End If
          End With
      
          'Reset
          Application.ScreenUpdating = True
          
      End Sub

       

      I received an error- Compile error:  End With without With

      Any idea if the above code will work or how to fix the error?

      • NikolinoDE's avatar
        NikolinoDE
        Gold Contributor

        ChicagoLane 

        I think it is a small mistake in the code structure. The error is due to the misplaced End With.

        Here is the corrected code:

        Vba Code

        Sub MoveCRLIJ()
            Dim fCell As Range
            Dim wsSearch As Worksheet
            Dim wsDest As Worksheet
            Dim lastRow As Long
            Dim ownerName As String
            Dim destTableName As String
            
            'What sheet are we searching?
            Set wsSearch = Worksheets("CR-LIJ")
            'Where should we move the data?
            Set wsDest = Nothing
            
            'Prevent screen flicker
            Application.ScreenUpdating = False
            
            'We will be searching col B
            With wsSearch.Range("B:B")
                'Find the word "X"
                Set fCell = .Find(what:=ownerName, LookIn:=xlValues, lookat:=xlWhole, MatchCase:=False)
                If Not fCell Is Nothing Then ' Check if the cell is not empty
                    ownerName = fCell.Value
                    destTableName = ownerName ' Adjust the table names as needed
                
                    ' Check if the destination worksheet exists
                    On Error Resume Next
                    Set wsDest = Worksheets(destTableName)
                    On Error GoTo 0
            
                    'Repeat until we've moved all the records
                    Do Until fCell Is Nothing
                        'Found something, copy and delete
                        'Where will we paste to?
                        lastRow = wsDest.Cells(wsDest.Rows.Count, "A").End(xlUp).Offset(1).Row
                        
                        'Copy A:AL
                        wsSearch.Cells(fCell.Row, "A").Resize(1, 38).Copy
                        wsDest.Cells(lastRow, "A").PasteSpecial Paste:=xlPasteValues
                 
                        fCell.EntireRow.Delete
                        
                        'Try to find next one
                        Set fCell = .Find(what:=ownerName, LookIn:=xlValues, lookat:=xlWhole, MatchCase:=False)
                    Loop
                    
                    'Resize our table to match new data
                    If lastRow <> 0 Then
                        wsDest.ListObjects.Add(xlSrcRange, wsDest.Range("A1").CurrentRegion, , xlYes).Name = "CRLIJWork"
                    End If
                End If
            End With
        
            'Reset
            Application.ScreenUpdating = True
        End Sub

        This should resolve the compile error. Additionally, I added a check If Not fCell Is Nothing before proceeding with further operations to avoid errors when Find does not locate the value.

  • ChicagoLane's avatar
    ChicagoLane
    Copper Contributor

    I just received this code from another forum that worked perfectly:

    Sub MoveCRLIJ()
         Dim V, R&, W
       With Sheet2.ListObjects(1)
            V = Filter(.Parent.Evaluate(Replace("TRANSPOSE(IF(#>0,ROW(#)-" & .Range.Row & "))", "#", .DataBodyRange.Columns(2).Address)), False, False)
        For R = 0 To UBound(V)
         If Evaluate("ISREF('" & .ListRows(V(R)).Range(2) & "'!A1)") Then
            W = .ListRows(V(R)).Range
       With Sheets(.ListRows(V(R)).Range(2).Text).Cells(Rows.Count, 1).End(xlUp)
           .Cells(2 + (.Text = "")).Resize(, UBound(W, 2)) = W
       End With
         Else
            V(R) = False
         End If
        Next
            V = Filter(V, False, False)
            For R = UBound(V) To 0 Step -1:  .ListRows(V(R)).Delete:  Next
       End With
    End Sub

Resources