sorting in VBA

wibni

New Member
Joined
Jun 15, 2011
Messages
33
Hello,

Below is my table I try to sort in VBA.
Column A are months and must not change.
Column B,C and D need to be sorted so that "Site" in Column B always comes on top.
There will only ever be "Office" or "Site" in column B but I'm having difficulties in finding a way to sort it by month name.

Would anyone here please be able to give me an idea on how to sort my table?

Excel Workbook
ABCD
1JanuarySiteGGM6
2JanuarySiteNMM5
3JanuaryOfficeMWA11
4JanuaryOfficeKNC11
5FebruarySiteGGM7
6FebruarySiteNMM5
7FebruaryOfficeMWA12
8FebruaryOfficeKNC12
9MarchSiteGGM7
10MarchSiteNMM5
11MarchOfficeMWA12
12MarchOfficeKNC12
13AprilSiteGGM6
14AprilSiteNMM7
15AprilOfficeMWA13
16AprilOfficeKNC13
Sheet1
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
This is what I got from the macro recorder, would think it can be neatened up, but think it does what you want

Code:
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("B2:B16") _
        , SortOn:=xlSortOnValues, Order:=xlAscending, CustomOrder:= _
        "January,February,March,April,May,June,July,August,September,October,November,December" _
        , DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("C2:C16") _
        , SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Sheet1").Sort
        .SetRange Range("B1:E16")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With

Obviously you'll need to change the sheet name and range to match your requirements
 
Last edited:
Upvote 0
Try

Code:
Sub ASort()
Dim LR As Long, i As Long
LR = Range("A" & Rows.Count).End(xlUp).Row
For i = 1 To LR - 3 Step 4
    Range("B" & i).Resize(4, 3).Sort Key1:=Range("B" & i), Order1:=xlDescending, _
        Header:=xlNo, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
Next i
End Sub
 
Upvote 0
Thanks for your replies.

VoG - How would I make the 'Step 4' in your code dynamic since the rows for each month are not always 4. They can be more or less 4.

I guess I need to have a counter for the number of rows per month somehow.


Noz2k - I tried that with the macro code as well but it sorts the whole range and not for every month.
 
Upvote 0
Try this

Code:
Sub ASort()
Dim LR As Long, i As Long, x As Long
x = Application.InputBox("Enter number of rows per month", Type:=1)
LR = Range("A" & Rows.Count).End(xlUp).Row
For i = 1 To LR - (x - 1) Step x
    Range("B" & i).Resize(x, 3).Sort Key1:=Range("B" & i), Order1:=xlDescending, _
        Header:=xlNo, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
Next i
End Sub
 
Upvote 0
Yeah, thats a good idea that works but I can't have input boxes.
Can I somehow count the rows per month and use this in the sort?
 
Upvote 0
Try

Code:
Sub ASort()
Dim LR As Long, i As Long, x As Long
LR = Range("A" & Rows.Count).End(xlUp).Row
For i = 1 To LR
    If Range("A" & i).Value <> Range("A" & i + 1).Value Then
        x = i
        Exit For
    End If
Next i
For i = 1 To LR - (x - 1) Step x
    Range("B" & i).Resize(x, 3).Sort Key1:=Range("B" & i), Order1:=xlDescending, _
        Header:=xlNo, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
Next i
End Sub
 
Upvote 0
Thank you very much!

I've done it now like the below and it seems towork ok.

Code:
LR = Range("A1").End(xlDown).Row
    x = 1
    SortStartRow = 1
    
    For i = x To LR
        If Range("A" & i).Value <> Range("A" & i + 1).Value Then
            Range("B" & SortStartRow).Resize(x, 13).Sort Key1:=Range("B" & SortStartRow), Order1:=xlDescending, _
            Header:=xlNo, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
            SortStartRow = i + 1
            x = 1
        Else
            x = x + 1
        End If
    Next i
 
Upvote 0

Forum statistics

Threads
1,224,609
Messages
6,179,875
Members
452,949
Latest member
Dupuhini

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