VBA Help - Sorting Code issues

Johnny Thunder

Well-known Member
Joined
Apr 9, 2010
Messages
675
Office Version
  1. 2016
Platform
  1. MacOS
Hello All,

I am having the weirdest issue right now. I have a piece of code that was working great and now the code seems to be not working correctly or just not doing anything at all.

Here is the code
VBA Code:
Dailylog.Sort.SortFields.Clear
With Dailylog.Range("E4:X" & LastR1) 'Does the sorting on a specific range
    .Sort Key1:=.Range("M3"), Order1:=xlAscending, Key2:=.Range("E3"), Order2:=xlAscending, Header:=xlYes
        .EntireColumn.AutoFit
End With

Not sure what happened to it but it was working fine before. Any ideas are helpful. This code is being triggered on Excel 2016 for Mac

This is the column of data that should be getting sorted but as you can see the data is unsorted.

Daily Budget Log v2.6.xlsm
M
3Date Received
48/30/20
510/15/20
69/15/20
7
8
Daily Log


Whenever I use this manually recorded script it runs just fine so what is the deal?


VBA Code:
Sub Macro2()


    ActiveSheet.Range("E3:X8").Select
    ActiveWorkbook.Worksheets("Daily Log").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Daily Log").Sort.SortFields.Add2 Key:=Range( _
        "M4:M8"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
        xlSortNormal
    ActiveWorkbook.Worksheets("Daily Log").Sort.SortFields.Add2 Key:=Range( _
        "E4:E8"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    With ActiveWorkbook.Worksheets("Daily Log").Sort
        .SetRange Range("E3:X8")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    
End Sub
 
Last edited:

Some videos you may like

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.

JLGWhiz

Well-known Member
Joined
Feb 7, 2012
Messages
12,979
Office Version
  1. 2013
Platform
  1. Windows
Rich (BB code):
With Dailylog.Range("E4:X" & LastR1)

The only thing I see is that this line would cause your Key1 reference to fall outside the sort area thus making it an invalid reference and also making your header row as row 4. Try changing the sort range to include row 3 with this modified line.

VBA Code:
With Dailylog.Range("E3:X" & LastR1)
 

Watch MrExcel Video

Forum statistics

Threads
1,122,354
Messages
5,595,672
Members
414,008
Latest member
zcaa0g

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
Top