Sort columns B,C,D based on column B then loop to next 3 columns and repeat till last column

Andy15

Board Regular
Joined
Apr 1, 2017
Messages
56
Hi Guy's I am looking for a little bit of help if possible.

I have a worksheet that contain numerous columns of data that has been imported from other worksheets.

The columns start at column B and repeat every 3 columns. The number of columns can vary per worksheet.

The number of rows can also vary within the blocks of 3 columns. (e.g. columns B,C D may have 20 rows. columns E,F,G may have 27 rows etc.)

I have managed to sort the first 3 columns using the code below, but can not work out how to loop through to the next 3 columns and so on until the last column.

Code:
Sub sort1()


Range("B21", Range("D" & Rows.Count).End(xlUp).Address).sort Key1:=[b21], _
    Order1:=xlAscending, Header:=xlYes
    
End Sub


I would welcome any help and advice.

Many thanks
Andy
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Your code is the same as this.

Code:
With Range("B:B")
    With Range(.Cells(21, 1), .Cells(Rows,Count, 3).End(xlup)
        .Sort Key1:=.Cells(21, 1), Order1:=xlAscending, Header:=xlYes
    End With
End With

Notice that the outer With is the only place that references the column, everything inside is relative to that column.

So you can use this loop

Code:
Dim ColNum As Long

For ColNum = 2 to .Cells(21, Columns.Count).End(xlToLeft).Column Step 3

    With Columns(ColNum)
        With Range(.Cells(21, 1), .Cells(Rows,Count, 3).End(xlup))
            .Sort Key1:=.Cells(21, 1), Order1:=xlAscending, Header:=xlYes
        End With
    End With

Next ColNum
 
Upvote 0
Hi

Thanks for you reply, I have tried your code but I get an invalid or unqualified reference and it is pointing to .Cells

Thanks
 
Upvote 0
Hi,

I then receive a wrong number of arguments error 450

highlighting this line

With Range(.Cells(21, 1), .Cells(Rows, Count, 3).End(xlUp))
Thanks<style type="text/css">p.p1 {margin: 5.0px 0.0px 5.0px 0.0px; font: 12.0px 'Times New Roman'}</style>
 
Upvote 0
Code:
With Range(.Cells(21, 1), .Cells(Rows[B][COLOR="#FF0000"].[/COLOR][/B]Count, 3).End(xlUp))
not
Code:
With Range(.Cells(21, 1), .Cells(Rows[COLOR="#FF0000"][B],[/B] [/COLOR]Count, 3).End(xlUp))
 
Upvote 0
Hi Mark858,

Thanks for your input, that works great. Can you tell me how to add a customOrder:- to the above as I keep getting named argument not found

Code:
Dim ColNum As Long

For ColNum = 2 To Cells(21, Columns.Count).End(xlToLeft).Column Step 3


    With Columns(ColNum)
        With Range(.Cells(21, 1), .Cells(Rows.Count, 3).End(xlUp))
            .sort Key1:=.Cells(21, 1), Order1:=xlAscending, CustomOrder:= _
        "10115960,902240,11241290,10080910,11241460,10237680,11241500,10818620,10005390,10237710,11242600,11241480,11241470,11241589,11241599,11241649,11241539,11241529,11241609,11247349,11241210,10774620,11241220,11241230,11241202", Header:=xlYes
        End With
    End With


Next ColNum
 
Upvote 0
Hi mikerickson,

Sorry for cross posting but I didn't know what it meant. After reading the reasons why I now understand the importance of not cross posting.

Andy15
 
Upvote 0

Forum statistics

Threads
1,214,646
Messages
6,120,717
Members
448,985
Latest member
chocbudda

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