MS Excel VBA Code to Sort a Column Based on Distance and then Group Values

OilEconomist

Active Member
Joined
Dec 26, 2016
Messages
421
Office Version
  1. 2019
Platform
  1. Windows
Thanks in advance for you assistance.
I have a set of data that I am trying to sort by four criteria in the following order:
(1) By distance
(2) Name
(3) Gender
(4) Yes or No

The issue is that some of the names are repeated so there is no way to group them together if one of the other columns like Distance is different. Take this small example, I would like to sort by Distance and name. Initially I have this:
Specialist Tool - (Active).xlsm
ABC
1NAMENEWDISTANCE
2Ron SmithYes2
3Jack FrostNo10
4Ron SmithYes20
Sheet2


Finally I would like this:
Specialist Tool - (Active).xlsm
ABC
1NAMENEWDISTANCE
2Ron SmithYes2
3Ron SmithYes20
4Jack FrostNo10
Sheet2


But there is no way to do this with sort because if you sort by distance, you could not group all the Ron Smith's together.

Any ideas? Please note the data size I am using is much larger (like 150+ rows)
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
You could use a helper column & sort on that 1st
Fluff.xlsm
ABCD
1NAMENEWDISTANCEhelp
2Ron SmithYes22
3Jack FrostNo1010
4Ron SmithYes202
Data
Cell Formulas
RangeFormula
D2:D4D2=MINIFS(C:C,A:A,A2)
 
Upvote 0
Solution
You could use a helper column & sort on that 1st
Fluff.xlsm
ABCD
1NAMENEWDISTANCEhelp
2Ron SmithYes22
3Jack FrostNo1010
4Ron SmithYes202
Data
Cell Formulas
RangeFormula
D2:D4D2=MINIFS(C:C,A:A,A2)
Thanks. That's a possibility. I was thinking of:
(1) Sorting by distance
(2) Then take the first value, in this case "Ron Smith", storing it in a string like "SortIt"
(3) whenever a line equaled "SortIt", moving it to the the top section after the previous SortIt.

This could run really slow and your idea might be the way to go.

I'm surprised nothing like this has been done previously or I just can't find it.
 
Upvote 0
It's certainly come up before. :)
 
Upvote 0
It's basically just what I suggested.
 
Upvote 0

Forum statistics

Threads
1,214,981
Messages
6,122,566
Members
449,089
Latest member
Motoracer88

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