Sort range based on value

lockarde

Board Regular
Joined
Oct 23, 2016
Messages
77
Good morning all,

I have an issue with a spreadsheet I use to track job details. Ideally, these details are supposed to be entered as they come in, but sometimes people wait, and suddenly there are details from several days or even weeks past. Is there a way to sort all of these details by the request date? I know native sorting exists, but I have merged cells so sorting isn't possible. I'm not really sure how to store each job range during the sorting? My spreadsheet void of sensitive information is below:
Daily Engineering Reporting - TESTING.xlsm
ABCDEFGHIJKLMNOPQRSTU
1Item(s)Request TypeLocationEmployeeJob IDCompany Job Value Job Cost Est HoursAct HoursReq DateComp DateLWHQty.Crate StyleMil Spec?Prod Job?Concept?Design/BOM?
2Item 1Type 14/7/20204/7/2020
3Request Details:Typical job contains 3 rows of information, column count never changes
4
5Item(s)Request TypeLocationEmployeeJob IDCompany Job Value Job Cost Est HoursAct HoursReq DateComp DateLWHQty.Crate StyleMil Spec?Prod Job?Concept?Design/BOM?
6Item 1Type 24/1/20204/3/2020
7Request Details:
8
9Item(s)Request TypeLocationEmployeeJob IDCompany Job Value Job Cost Est HoursAct HoursReq DateComp DateLWHQty.Crate StyleMil Spec?Prod Job?Concept?Design/BOM?
10Item 1Type 34/1/20204/6/2020
11Request Details:
12
13Item(s)Request TypeLocationEmployeeJob IDCompany Job Value Job Cost Est HoursAct HoursReq DateComp DateLWHQty.Crate StyleMil Spec?Prod Job?Concept?Design/BOM?
14Item 1Type 24/3/20204/7/2020
15Request Details:Rabble rabble details details
16Notes:Sometimes jobs contain 4 rows, with notes
17
18Item(s)Request TypeLocationEmployeeJob IDCompany Job Value Job Cost Est HoursAct HoursReq DateComp DateLWHQty.SpecMil Spec?Prod Job?Concept?Design/BOM?
19Item 1Type 44/2/20204/3/2020
20Request Details:
Sheet1


This is what I'm going for, each job range is sorted by the request date:
Daily Engineering Reporting - TESTING.xlsm
ABCDEFGHIJKLMNOPQRSTU
25Item(s)Request TypeLocationEmployeeJob IDCompany Job Value Job Cost Est HoursAct HoursReq DateComp DateLWHQty.Crate StyleMil Spec?Prod Job?Concept?Design/BOM?
26Item 1Type 24/1/20204/3/2020
27Request Details:
28
29Item(s)Request TypeLocationEmployeeJob IDCompany Job Value Job Cost Est HoursAct HoursReq DateComp DateLWHQty.Crate StyleMil Spec?Prod Job?Concept?Design/BOM?
30Item 1Type 34/1/20204/6/2020
31Request Details:
32
33Item(s)Request TypeLocationEmployeeJob IDCompany Job Value Job Cost Est HoursAct HoursReq DateComp DateLWHQty.SpecMil Spec?Prod Job?Concept?Design/BOM?
34Item 1Type 44/2/20204/3/2020
35Request Details:
36
37Item(s)Request TypeLocationEmployeeJob IDCompany Job Value Job Cost Est HoursAct HoursReq DateComp DateLWHQty.Crate StyleMil Spec?Prod Job?Concept?Design/BOM?
38Item 1Type 24/3/20204/7/2020
39Request Details:Rabble rabble details details
40Notes:Sometimes jobs contain 4 rows, with notes
41
42Item(s)Request TypeLocationEmployeeJob IDCompany Job Value Job Cost Est HoursAct HoursReq DateComp DateLWHQty.Crate StyleMil Spec?Prod Job?Concept?Design/BOM?
43Item 1Type 14/7/20204/7/2020
44Request Details:Typical job contains 3 rows of information, column count never changes
Sheet1

How the jobs are sorted if they have the same request date isn't important, that can be any order.
I know this is a bit of a bear, so any help is greatly appreciated!
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
I finally figured this out. I haven't completely applied it to my actual tracking spreadsheet, there will be some tweeks, but I got it working great in my test sheet. What I ended up doing is searching through the spreadsheet and putting dates, and the address from .Find into a 2D array. Then I sorted that array, and as it goes through and sorts, once a value is at its "final" location in the array, it copies the rows in question to a 'temp' sheet. Once all the rows are in the temp sheet, in the correct order, it copies everything over into the tracking sheet. My code is below:
VBA Code:
Sub DateSort()
Dim actsheet As Worksheet, rqdate As String, job As Range
Dim Rdate As Range, dates() As String, t As Integer, y As Integer
Dim r As Integer, actname As String, itemfind As Range, temp As String
'Dim jobadd As Address
Application.ScreenUpdating = False
Set actsheet = ActiveSheet
actname = actsheet.Name
Sheets("Temp").UsedRange.Delete
    With Sheets(actname)
        ReDim dates(.Cells(1, 8).Value - 1, 1) As String
        Set Rdate = .Cells.Find("Req Date")
        Set job = Intersect(Range(Rdate.Offset(0, -9), .Cells(Rdate.Row + 2, _
            Rdate.Column + 10)), Range("A:U"))
        rqdate = Rdate.Offset(1, 0).Value
        firsjob = Rdate.Address
        r = 0

        Do
             
            rqdate = Rdate.Offset(1, 0).Value
            Set job = Intersect(Range(Rdate.Offset(0, -9), Rdate.Offset(0, -9). _
                End(xlDown).EntireRow), Range("A:T"))
            If r = 0 Then
                dates(0, 0) = rqdate
                dates(0, 1) = Rdate.Address
                r = r + 1
            Else
                dates(r, 0) = rqdate
                dates(r, 1) = Rdate.Address
                r = r + 1
            End If

            Set Rdate = .Cells.FindNext(Rdate)
            
        Loop While Rdate.Address <> firsjob
Application.StatusBar = "Sorting jobs....."
    For t = 0 To r Step 1
        For y = t + 1 To r - 1 Step 1
            If day(dates(y, 0)) < day(dates(t, 0)) Then
                With Sheets(actname)
                    temp = dates(t, 0)
                    tempadd = dates(t, 1)
                    dates(t, 0) = dates(y, 0)
                    dates(t, 1) = dates(y, 1)
                    dates(y, 0) = temp
                    dates(y, 1) = tempadd
                End With
            End If
        Next y
        With Sheets(actname)
            If y <= 23 Then
                lastrow = Sheets("Temp").Cells(Sheets("Temp").Rows.Count, "A").End(xlUp).Row
                Set job = Intersect(Range(.Range(dates(t, 1)).Offset(0, -9), .Range(dates(t, 1)).Offset(0, -9). _
                    End(xlDown).EntireRow), .Range("A:T"))
                job.Copy: Sheets("Temp").Cells(lastrow + 2, 1).PasteSpecial Paste:=8, Operation:=xlNone
                Sheets("Temp").Cells(lastrow + 2, 1).PasteSpecial Paste:=-4104, Operation:=xlNone
            End If
        End With
    Next t

    End With
    Sheets(actname).UsedRange.Offset(3, 0).Delete
    Sheets("Temp").UsedRange.Copy: Sheets(actname).Cells(4, 1).PasteSpecial Paste:=8, Operation:=xlNone
    Sheets(actname).Cells(4, 1).PasteSpecial Paste:=-4104, Operation:=xlNone
    Application.CutCopyMode = False
    Sheets(actname).Range("H1").Formula = "=CountIf(A:A, ""Item(s)"")"
    Sheets(actname).Range("M2").Formula = "=SUM(I4:I250) & "" / "" & SUM(H4:H250)"
Application.StatusBar = "Sorting Complete."
Application.StatusBar = ""
Sheets(actname).Range("A1").Select
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,183
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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