VBA Help - Sorting Data Table

Johnny Thunder

Well-known Member
Joined
Apr 9, 2010
Messages
693
Office Version
  1. 2016
Platform
  1. MacOS
Hello All,

I am working on a table of data that I will need to sort by the "Object" Column. The goal is to have a button that the user can trigger that will loop thru the data and find any new values and sort the data accordingly and also put a under line border on the last unique value of each group of values.

Any help is always appreciated!

Here is a sample of unsorted data as the user populates the table -
Budget Log V1.0.xlsm
ABCDEFGHIJKLMNO
1Daily Log
2
3Row CountObjectDate Rec'DBudget Reviewed ByDate ReviewedNotes Given/ ApprovedCurrencyBudget Net/ EpTotal Gross BudgetLess Est.Tax IncentivesBudget AdjTotal Net BudgetChanges From Last DraftArrowsComments
41Orange9/6/20Billy9/21/20USD 500 700 575 550 ?
52Apple9/11/20Billy9/26/20USD 650 850 725 700 ?
63Apple9/16/20Billy10/1/20USD 800 1,000 875 850 ?
75Pear9/26/20Billy10/11/20USD 1,100 1,300 1,175 1,150 ?
86Banana10/1/20Billy10/16/20USD 1,250 1,450 1,325 1,300 ?
97Pear10/6/20Billy10/21/20USD 1,400 1,600 1,475 1,450 ?
108Banana10/11/20Billy10/26/20USD 1,550 1,750 1,625 1,600 ?
Test



And here is a sample after the data has been manipulated by a macro (hopefully) -
Budget Log V1.0.xlsm
ABCDEFGHIJKLMNO
16Daily Log
17
18Row CountObjectDate Rec'DBudget Reviewed ByDate ReviewedNotes Given/ ApprovedCurrencyBudget Net/ EpTotal Gross BudgetLess Est.Tax IncentivesBudget AdjTotal Net BudgetChanges From Last DraftArrowsComments
191Apple9/11/20Billy9/26/20USD 650 850 725 700 ?
202Apple9/16/20Billy10/1/20USD 800 1,000 875 850 ?
213Banana10/1/20Billy10/16/20USD 1,250 1,450 1,325 1,300 ?
224Banana10/11/20Billy10/26/20USD 1,550 1,750 1,625 1,600 ?
235Orange9/6/20Billy9/21/20USD 500 700 575 550 ?
247Pear9/26/20Billy10/11/20USD 1,100 1,300 1,175 1,150 ?
258Pear10/6/20Billy10/21/20USD 1,400 1,600 1,475 1,450 ?
Test
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Let's get the ball rolling. If you have any questions let us know. Maybe some of thee A Students will weigh in and shorten the program.


VBA Code:
Sub Sort1()
Dim LastRow As Long
Sheets("Sort Obj").Select

LastRow = Cells(Rows.Count, "B").End(xlUp).Row

Range((Cells(3, 1)), (Cells(LastRow, 15))).Select
    ActiveWorkbook.Worksheets("Sort Obj").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Sort Obj").Sort.SortFields.Add2 Key:=Range((Cells(4, 2)), (Cells(LastRow, 2))) _
        , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Sort Obj").Sort
        .SetRange Range((Cells(3, 1)), (Cells(LastRow, 15)))
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    Range("B2").Select
    
 For row1 = 4 To LastRow
    
 If Cells(row1, 2) <> Cells(row1 + 1, 2) Then
    Range((Cells(row1, 1)), (Cells(row1, 15))).Select

    With Selection.Borders(xlEdgeBottom)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
    
End If

Next row1
Range("A1").Select

End Sub

20-09-21 Sort Object.xlsm
ABCDEFGHIJKLMNO
1Daily Log
2
3Row CountObjectDate Rec'DBudget Reviewed ByDate ReviewedNotes Given/ ApprovedCurrencyBudget Net/ EpTotal Gross BudgetLess Est.Tax IncentivesBudget AdjTotal Net BudgetChanges From Last DraftArrowsComments
41Orange9/6/2020Billy9/21/2020USD500700575550?
52Apple9/11/2020Billy9/26/2020USD650850725700?
63Apple9/16/2020Billy10/1/2020USD8001000875850?
75Pear9/26/2020Billy10/11/2020USD1100130011751150?
86Banana10/1/2020Billy10/16/2020USD1250145013251300?
97Pear10/6/2020Billy10/21/2020USD1400160014751450?
108Banana10/11/2020Billy10/26/2020USD1550175016251600?
Sort Obj
 
Upvote 0
Thank you for the help on this. Will be giving this a shot shortly. I will update on how it works for my project.
 
Upvote 0
Count

Code:
=IF(B4<>"",ROWS($A4:A$4),"")

in Worksheet module
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Sht As Worksheet
Set Sht = ThisWorkbook.Worksheets("Sort Obj")

    With Sht
        .Sort.SortFields.Clear
        .Sort.SortFields.Add2 Key:=Range("B4"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        With .Sort
            .SetRange Range("A4:O20")
            .Header = xlNo
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
    End With

End Sub

Book1
ABCDEFGHIJKLMNO
1Daily Log
2
3Row CountObjectDate Rec'DBudget Reviewed ByDate ReviewedNotes Given/ ApprovedCurrencyBudget Net/ EpTotal Gross BudgetLess Est.Tax IncentivesBudget AdjTotal Net BudgetChanges From Last DraftArrowsComments
41Apple9/11/2020Billy9/26/2020USD650850725700?
52Apple9/16/2020Billy10/1/2020USD8001000875850?
63Banana10/1/2020Billy10/16/2020USD1250145013251300?
74Banana10/11/2020Billy10/26/2020USD1550175016251600?
85Orange9/6/2020Billy9/21/2020USD500700575550?
96Pear9/26/2020Billy10/11/2020USD1100130011751150?
107Pear10/6/2020Billy10/21/2020USD1400160014751450?
Sort Obj
Cell Formulas
RangeFormula
A9:A10,A4:A7A4=IF(B4<>"",ROWS($A$4:A4),"")
A8A8=IF(B8<>"",ROWS($A4:A$8),"")
 
Upvote 0

Forum statistics

Threads
1,214,951
Messages
6,122,442
Members
449,083
Latest member
Ava19

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