advenced sort by calculation result - VBA

zico8

Board Regular
Joined
Jul 13, 2015
Messages
227
Hi,

I need your help to find any idea how to handle the task.

Let's say - I have the sheet with 500 rows (from the top) where column A contains Name and column B and column C contain some numbers.

row1: name1, 2, 3,
row2: name2, 1, 1,
row3: name3, 3, 1,
row4: name4, 2, 4,
and so on.

I need to sort these rows that way so the total sum of (column B - Column C) * Row() will be the lowest.

In the case above the correct result is:
row1: name3, 3, 1,
row2: name2, 1, 1,
row3: name1, 2, 3,
row4: name4, 2, 4,

The result is:
(3-1)*1+(1-1)*2+(2-3)*3+(2-4)*4 = -9 and will not be lowest in any of other sorting.

How to do it by VBA?
The result can be calculated by formula in the sheet or inside in the macro.

Is it possible to do it so the executing macro will not take whole day??
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Hi,

I need your help to find any idea how to handle the task.

Let's say - I have the sheet with 500 rows (from the top) where column A contains Name and column B and column C contain some numbers.

row1: name1, 2, 3,
row2: name2, 1, 1,
row3: name3, 3, 1,
row4: name4, 2, 4,
and so on.

I need to sort these rows that way so the total sum of (column B - Column C) * Row() will be the lowest.

In the case above the correct result is:
row1: name3, 3, 1,
row2: name2, 1, 1,
row3: name1, 2, 3,
row4: name4, 2, 4,

The result is:
(3-1)*1+(1-1)*2+(2-3)*3+(2-4)*4 = -9 and will not be lowest in any of other sorting.

How to do it by VBA?
The result can be calculated by formula in the sheet or inside in the macro.

Is it possible to do it so the executing macro will not take whole day??

the only way i would think to do this without actually calculating all 500 rows would be to sort column B by smallest value and then column C by largest value
Code:
Sub Macro1()
Dim lastRow As Long

lastRow = Range("A" & Rows.Count).End(xlUp).Row

    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("B2:B" & lastRow) _
        , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("C2:C" & lastRow) _
        , SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Sheet1").Sort
        .SetRange Range("A1:C" & lastRow)
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End Sub

see if it works
change "Sheet1" to whatever your sheet name is called
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
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