Sorting Columns

BiochemBoi95

New Member
Joined
Jan 22, 2020
Messages
20
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
  2. Web
I am trying to figure out a way to sort each pair of "Bendability" and "Curvature" columns based on the "Bendability" columns values going from low to high. I have many columns of data and if there is a more automatic way of doing this, that would be awesome.

Here is an example of my data:

PositionBendabilityPositionBendabilityPositionBendabilityPositionBendability
29​
5.5984​
29​
2.3966​
29​
4.4758​
29​
3.2822​
30​
6.3233​
42​
4.4428​
30​
2.2066​
30​
3.7015​
31​
6.6904​
41​
4.8421​
31​
4.0335​
31​
5.1261​
32​
6.0579​
34​
5.1842​
32​
7.7086​
32​
5.2788​
33​
6.195​
39​
5.1842​
33​
6.8403​
33​
4.8691​
34​
5.976​
44​
5.4089​
34​
5.5164​
34​
2.5104​
35​
5.0215​
35​
5.4162​
35​
5.4397​
35​
4.0335​
36​
4.527​
45​
5.4856​
36​
5.4089​
36​
7.7086​
37​
2.2066​
33​
5.5164​
37​
5.4856​
37​
6.8403​
38​
4.0335​
46​
5.5164​
38​
5.5164​
38​
5.5164​
39​
6.6037​
30​
5.7576​
39​
5.1842​
39​
7.0673​
40​
6.0579​
43​
5.827​
40​
5.9708​
40​
7.9188​
41​
4.8421​
40​
5.9708​
41​
4.8421​
41​
7.2362​
42​
6.0705​
38​
5.976​
42​
6.0705​
42​
7.7171​
43​
7.4868​
32​
6.5263​
43​
7.4868​
43​
5.3879​
44​
4.8802​
36​
6.6082​
44​
5.8914​
44​
4.0247​
45​
4.848​
31​
7.072​
45​
5.8914​
45​
5.4856​
46​
5.827​
37​
7.2998​
46​
4.8802​
46​
5.5164​

Here is an example of how I want it all sorted


PositionBendability
37​
2.2066​
38​
4.0335​
36​
4.527​
41​
4.8421​
45​
4.848​
44​
4.8802​
35​
5.0215​
29​
5.5984​
46​
5.827​
34​
5.976​
32​
6.0579​
40​
6.0579​
42​
6.0705​
33​
6.195​
30​
6.3233​
39​
6.6037​
31​
6.6904​
43​
7.4868​

Thanks in advance.
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Try this:

VBA Code:
Sub Sorting_Columns()
  Dim i As Long
  Application.ScreenUpdating = False
  For i = 1 To Cells(1, Columns.Count).End(1).Column Step 2
    Cells(1, i).Resize(Cells(Rows.Count, i).End(3).Row, 2).Sort Cells(1, i + 1), xlAscending, Header:=xlYes
  Next
End Sub

HOW TO INSTALL MACROs
------------------------------------
If you are new to macros, they are easy to install and use. To install it, simply press ALT+F11 to go into the VB editor and, once there, click Insert/Module on its menu bar, then copy/paste the above code into the code window that just opened up. That's it.... you are done. To use the macro, go back to the worksheet with your data on it and press ALT+F8, select the macro name (Sorting_Columns) from the list that appears and click the Run button. The macro will execute and perform the action(s) you asked for. If you will need to do this again in this same workbook, and if you are using XL2007 or above, make sure you save your file as an "Excel Macro-Enabled Workbook (*.xlsm) and answer the "do you want to enable macros" question as "Yes" or "OK" (depending on the button label for your version of Excel) the next time you open your workbook.
 
Upvote 0
Thanks so much it worked great! Would you know how to do the opposite and now sort based on the position columns?
 
Upvote 0
Change this

Cells(1, i + 1),

To this

Cells(1, i),
 
Upvote 0
I'm glad to help you. Thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,039
Messages
6,122,799
Members
449,095
Latest member
m_smith_solihull

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