Advertisement
Guest User

Sorting Rows By Tables & Sheets

a guest
May 9th, 2025
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VisualBasic 1.61 KB | Software | 0 0
  1.  
  2. Option Explicit
  3. Private Sub Worksheet_Change(ByVal Target As Range)
  4. Application.EnableEvents = False
  5. If Target.Cells.Count > 1 Then
  6. GoTo done
  7.            If Application.Intersect(Target, ActiveSheet.Range("G:G")) Is Nothing Then
  8. GoTo done
  9.            If Target.Value <> "Hold" And Target.Value <> "Yes" And Target.Value <> "No" And Target.Value <> "Eat" Then
  10. GoTo done
  11.    
  12.         MoveRowInTable Target.Value
  13.            Dim i As Long
  14.             For i = 1 To Target.Count
  15.    
  16.         If Target(i).Value > 0 Then
  17.             MoveRowsInTableTarget(i).Value
  18.    
  19.         End If
  20.       Next
  21. done:     Application.EnableEvents = True
  22.    End Sub
  23.  
  24. Module1
  25. ------
  26. Sub MoveRowsInTable()
  27.  
  28.     Dim sourceTable As ListObject
  29.     Dim destinationTable As ListObject
  30.     Dim selectedRow As ListRow
  31.     Dim lastRow As ListRow
  32.     Dim Cell As Variant
  33.    
  34.     Set sourceTable = Worksheets("Unpaid").ListObjects("Table1")
  35.     Set selectedRow = sourceTable.ListRows(Selection.Row - sourceTable.HeaderRowRange.Row)
  36.    
  37. If Cell = "Yes" Then
  38.             Set destinationTable = Worksheets("Paid").ListObjects("Table2")
  39.        ElseIf Cell = "No" Then
  40.             Set destinationTable = Worksheets("Collect").ListObjects("Table3")
  41.         ElseIf Cell = "Eat" Then
  42.             Set destinationTable = Worksheets("Credit").ListObjects("Table4")
  43. End If
  44.  
  45.     lastRow = destinationTable.ListRows.Add
  46.    
  47.     lastRow.Range.Value = selectedRow.Range.Value
  48.    
  49.     selectedRow.Delete
  50. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement