-1

I need to organize transaction information in a spreadsheet by ascending date order. Each row represents a single person and the last 6 transactions in their history. The problem is that while the database spits out the last 6 transactions, it doesn't do so in order, so I need to do it manually in Excel.

The transaction date and expiration date must stay tied to the "Is Gift" and "Action" data, so using row sorting functions doesn't work for this application. These four columns need to be kept together as a group.

In the attached screenshot, I've used colors to show how columns need to remain grouped together and highlighted in red, the transaction that SHOULD have come first if the data were in the correct order. Hopefully this gives a clearer indication of the issue ("firsts" are in different columns for every individual) and what my goal is (for each column group to be sorted, in ascending date order, by row).

Screenshot with notes

2
  • 1
    The screenshot is so big and vague, I'm not able to read it. Do you not have some small piece of sample data, current output, desired output and your current attempt? That would really help
    – JvdV
    Commented Jul 18, 2019 at 20:22
  • Thank you, JvdV for your feedback. I'll edit my original post and upload a new screenshot to try to make my request more clear.
    – Ashley
    Commented Jul 21, 2019 at 15:02

1 Answer 1

0

Try this code. Paste this in VB Editor window and run it (google 'how to run vba code' for more help). This is not at all efficient, but it should work.

Sub Some_Sorting_Procedure()
    Dim r As Long, last_row As Long
    Dim i As Long, j As Long
    Dim arr As Variant

    Application.ScreenUpdating = False

    With ActiveSheet
        last_row = .Cells(.Rows.Count, 1).End(xlUp).Row
        For r = 2 To last_row
            For i = 3 To 23 Step 4
                For j = i + 4 To 23 Step 4
                    If .Cells(r, i).Value > .Cells(r, j).Value Then
                        arr = .Cells(r, j).Resize(, 4).Value
                        .Cells(r, i).Resize(, 4).Cut .Cells(r, j)
                        .Cells(r, i).Resize(, 4).Value = arr
                    End If
                Next j
            Next i
        Next r
    End With
End Sub

Edit

This assumes that your data starts at cell A1, and there are no empty cells in the first column. The hard-coded value 23 is because you mentioned there would be only six transactions. You'll need to increase this number by 4 for each extra transaction.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .