Mind Tickler

jswhip

Board Regular
Joined
Jan 19, 2005
Messages
69
I am looking for vba code to sort columns based upon values in another set of columns. The number of columns will change row by row, thus it needs to be dynamic in nature.

For example, the first row may have the following fields in columns A-H:

Field_A, Field_B, Field_C, Field_D, 3, 2, 7, 1 (where the 4 numeric values correspond to the first 4 fields).

Thus, I would like to sort columns A-D based upon the values in E-H. I.E - the output columns should be:
Field_D, Field_B, Field_A, Field_C, 1, 2, 3, 7

I thought these could be loaded into a dynamic two dimensional array and then sorting, but not sure how to go about doing it. Open to other suggestions as well if there is a better way.
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
You need to sort backwards, starting at the rightmost column, going backwards. The macro recorder will give you the code.
 
Upvote 0
Thanks for replying - appreciate it.

Not sure if I follow "....sort backwards, starting at the rightmost column, going backwards." Would you elaborate?

Thank you.
 
Upvote 0
OK. Excel's sort feature only allows you to pick up to 3 columns to sort by - here is how to get around this. The trick is sorting by the last key first and working back to the first key. Say your data is in Columns A:E and you want to sort by A, B, C, D then E:


  1. Select all of Columns A:E
  2. Go to Data>Sort> sort by C then by D then by E
  3. Click Sort
  4. Now again with Columns A:E selected
  5. Go to Data>Sort> sort by A then by B
  6. Click Sort
 
Upvote 0

Forum statistics

Threads
1,223,061
Messages
6,169,873
Members
452,287
Latest member
winnievmex

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