Cutting and Pasting Rows Based on a Matching Criteria

LukeAJN

New Member
Joined
Feb 7, 2022
Messages
11
Hi All

I have a particular problem here that I am wondering if anyone can figure out the best way to go about it. I currently have a spreadsheet with a jumbled and out of order list of values in a certain column. I need to be able to search the spreadsheet, find the two corresponding entries, and then cut and paste them so that they are in rows next to each other. The data is already sorted largest to smallest so when cutting a row it must always best inserted below the corresponding entries.


A small visualization of this would be like the below




COL B
X1
Y2
Z1
Y1
Z2
X2

This would then become


COL B
X1
X2
Y2
Y1
Z1
X2
 
I think I have a working solution - here are the steps
1) Sorts the rows by the amount largest to smallest
2) Moves all the Primary account numbers to the top of the list
3) Moves (pairs) the Secondary account number with Primary account number
4) Dependent upon creating a paired list (another sheet) matching Primary and Secondary Account numbers

Please provide a larger data set for testing purposes.

Before (after sorting)
OrderLinkedValues.xlsm
ABC
1Account NumberCCYAmount
210072023EUR1,242,721,331.69
34320017392EUR444,744,495.06
44320018304EUR430,654,789.01
54370018398JPY63,232,200.00
64320005807USD47,134,293.89
74370042912EUR42,356,879.06
810001955USD39,204,045.00
910006247JPY36,133,093.89
Sheet1


After (after pairing)
OrderLinkedValues.xlsm
ABC
1Account NumberCCYAmount
24320017392EUR444,744,495.06
34370042912EUR42,356,879.06
44320018304EUR430,654,789.01
510072023EUR1,242,721,331.69
64370018398JPY63,232,200.00
710006247JPY36,133,093.89
84320005807USD47,134,293.89
910001955USD39,204,045.00
Sheet1


It is dependent on creating a pair list (I have it on another sheet name "Linked_Accounts")
OrderLinkedValues.xlsm
AB
1Primary AcctSecondary Acct
2432001830410072023
343200173924370042912
4437001839810006247
5432000580710001955
Linked_Accounts


Here is the first version of the VBA code
VBA Code:
Option Explicit
Sub GroupLinkedAccounts()
'
' Group Linked Accounts
'
  Dim lrSrc As Long, lrLA As Long
  Dim r As Long
  Dim rPrimary As Long, rSecondary As Long
  Dim rLastPrimary As Long
  Dim rng As Range, rngLA As Range
  Dim rngLA2 As Range
  Dim rngprev As Range
  Dim rngSrc As Range
  Dim wksLA As Worksheet 'Linked Accounts worksheet
  Dim wksSrc As Worksheet
  Dim Acct1, idx
  Dim rPrevIsPrimary As Long, rIsPrimary As Long

 
  Set wksSrc = ActiveSheet
  Set wksLA = Worksheets("Linked_Accounts")
  lrSrc = wksSrc.Range("A" & Rows.Count).End(xlUp).Row
  lrLA = wksLA.Range("A" & Rows.Count).End(xlUp).Row
  Set rngLA = wksLA.Range("A2:A" & lrLA)
  Set rngLA2 = wksLA.Range("B2:B" & lrLA)
'
  SortAmountMaxToMin 'separate macro/sub

  rPrimary = 1
  rLastPrimary = 1
  rSecondary = 2
  For r = 3 To lrSrc
    Range("A" & r & ":C" & r).Select
    'Is the value in Row r a Primary account number
    'find account number in linked account list
'    On Error GoTo end_of_loop_1
    On Error Resume Next
    With WorksheetFunction
      Set rngprev = wksSrc.Range("A" & r - 1 & ":C" & r - 1)
      rPrevIsPrimary = 0
      rPrevIsPrimary = .Match(rngprev(1), rngLA, 0)
      rIsPrimary = 0
      rIsPrimary = .Match(Selection(1), rngLA, 0)
      If rIsPrimary > 0 Then rLastPrimary = r
    End With
    If Not rPrevIsPrimary And rIsPrimary Then
      rPrimary = rPrimary + 1
      Selection.Cut
      wksSrc.Range("A" & rPrimary & ":C" & rPrimary).Select
      Selection.Insert Shift:=xlDown
    End If
end_of_loop_1:
    'Debug.Print Err.Number, Err.Description
  Next r
 
  'Now match up Primary and Secondary Accounts
  'All the primary accounts are at the top of the list
 
  'rPrimary - 1 (minus one) should be the last Primary record in the list
  'if there are no primary accounts in the list exit
  If rLastPrimary = 0 Then Exit Sub
 
  Set rngSrc = wksSrc.Range("A2:A" & lrSrc)

  For r = rLastPrimary To lrSrc
    Range("A" & r & ":C" & r).Select
    On Error Resume Next
    With WorksheetFunction
      idx = 0
      Acct1 = 0
      idx = .Match(Selection(1), rngLA2, 0)
      If idx > 0 Then
        Acct1 = .Index(rngLA, idx)
      End If
    End With
    If Acct1 > 0 Then
      rPrimary = 0
      rPrimary = WorksheetFunction.Match(Acct1, rngSrc, 0) + 1
      If rPrimary > 0 Then
        rSecondary = rPrimary + 1
        Selection.Cut
        wksSrc.Range("A" & rSecondary & ":C" & rSecondary).Select
        Selection.Insert Shift:=xlDown
      End If
    End If
  Next r
End Sub

Sub SortAmountMaxToMin()
'
' SortAmountMaxToMin
'

'
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("C2:C9"), _
        SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Sheet1").Sort
        .SetRange Range("A1:C9")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End Sub
If sorting is not needed and all you need is to pair accounts in adjacent rows here is a faster and much shorter version of the pairing code
VBA Code:
Option Explicit
Sub QuickAccountPairing()
'
' Group Linked Accounts
'
  Dim lrSrc As Long, lrLA As Long
  Dim r As Long, rmax As Long
  Dim rPrimary As Long, rSecondary As Long
  Dim rngLA As Range, rngLA2 As Range
  Dim rngSrc As Range
  Dim wksLA As Worksheet 'Linked Accounts worksheet
  Dim wksSrc As Worksheet
  Dim Acct1

  
  Set wksSrc = ActiveSheet
  Set wksLA = Worksheets("Linked_Accounts")
  lrSrc = wksSrc.Range("A" & Rows.Count).End(xlUp).Row
  lrLA = wksLA.Range("A" & Rows.Count).End(xlUp).Row
  Set rngLA = wksLA.Range("A2:A" & lrLA)
  Set rngLA2 = wksLA.Range("B2:B" & lrLA)
  Set rngSrc = wksSrc.Range("A2:A" & lrSrc)
'
  r = 2
  rmax = 2
  Do While r <= lrSrc
    Range("A" & r & ":C" & r).Select
    On Error Resume Next
    With WorksheetFunction
      rPrimary = 0
      rPrimary = .Match(Selection(1), rngLA, 0)
      rSecondary = 0
      If rPrimary = 0 Then 'Acct must be secondary
        Acct1 = rngLA(.Match(Selection(1), rngLA2, 0))
        rSecondary = .Match(Acct1, rngSrc, 0) + 2
        If r = rSecondary Then
          r = r + 1
        Else
          Selection.Cut
          wksSrc.Range("A" & rSecondary & ":C" & rSecondary).Select
          Selection.Insert Shift:=xlDown
        End If
      Else
        r = r + 1
      End If
    End With
  Loop

End Sub

Before (Sorted by Account Number smallest to largest)
OrderLinkedValues.xlsm
ABC
1Account NumberCCYAmount
210001955USD39,204,045.00
310006247JPY36,133,093.89
410072023EUR1,242,721,331.69
54320005807USD47,134,293.89
64320017392EUR444,744,495.06
74320018304EUR430,654,789.01
84370018398JPY63,232,200.00
94370042912EUR42,356,879.06
Sheet1


After Pairing
OrderLinkedValues.xlsm
ABC
1Account NumberCCYAmount
24320005807USD47,134,293.89
310001955USD39,204,045.00
44320017392EUR444,744,495.06
54370042912EUR42,356,879.06
64320018304EUR430,654,789.01
710072023EUR1,242,721,331.69
84370018398JPY63,232,200.00
910006247JPY36,133,093.89
Sheet1
 
Upvote 0

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.

Forum statistics

Threads
1,215,215
Messages
6,123,668
Members
449,114
Latest member
aides

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