VBA Sort Col's Independent of One Another

RLCornish

New Member
Joined
Feb 11, 2014
Messages
42
Hello,

I'm trying to figure out how to sort columns independently of one another using VBA, and I have a good little piece of code that does this for me (see below). However, I've run into a couple of other things.


  1. Some columns may need a custom sort order, not just ascending. What does something like this look like?
  2. I have 2 rows at the top, not just 1, that need to not be included in the sort. My code is working perfectly to exclude row 1, but grabs row 2 in the sort and I need it not to. Any ideas how to accomplish this?

It seems like this should be so easy, but I've played enough to know I don't know where I need to head with this. Help please!

Here's the code I have...copied from ozgrid

Sub SortColumsIndividually()
Dim rCell As Range

For Each rCell In Range("A1:D1") 'Change range to suit
rCell.EntireColumn.Sort Key1:=rCell(2, 1), _
Order1:=xlAscending, Header:=xlYes
Next rCell

End Sub
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Hi,

I think this works:
Rich (BB code):
Sub SortColumsIndividually()
    Dim rCell As Range
    
    For Each rCell In Range("A1:D1") 'Change range to suit
        rCell.EntireColumn.Resize(Rows.Count - 1).Offset(1).Sort Key1:=rCell.Cells(1, 1), Order1:=xlAscending, Header:=xlYes
    Next rCell
End Sub
The problem was the usage of EntireColumn. Using that made the VBA ignore the start row you entered anywhere else.

rCells.Cells(1,1) is really there just to set the column so changing the row there makes no difference.

What I have done is added an Offset of 1 row to move the starting point down a row and then Resized the column to make sure that it still finishes at row 1048756 (in my version of Excel).

To start a row further down you would use:
Rich (BB code):
rCell.EntireColumn.Resize(Rows.Count - 2).Offset(2).Sort Key1:=rCell.Cells(1, 1), Order1:=xlAscending, Header:=xlYes
I hope that makes sense.


Regards,
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,905
Messages
6,175,297
Members
452,633
Latest member
DougMo

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