Sort Record Macro works, then in VBA Call has ".Apply" Error

Shiseiji

Board Regular
Joined
Oct 23, 2009
Messages
214
Office Version
  1. 2019
Platform
  1. Windows
Simple macro, works just fine when I'm recording it, and looks like what I've used in the past. I've tried a couple of minor modifications, still and getting an error. Obviously I'm missing "something."
TIA!!

Code:
Sub Sort_XDATES1()
'
' tm_Sort_XDATES Macro
'
Application.Volatile True
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    Application.DisplayStatusBar = True
    '
    Dim LastRow                      As Long
    Dim LastCol                       As Integer
    Dim thiswksht                    As Worksheet
    '
    '
    Sheet2.Activate
    Set thiswksht = ActiveSheet
    '
    If thiswksht.AutoFilterMode Then
        AutoFilterMode = False
    End If
    '
    With thiswksht
        LastRow = Cells(Cells.Rows.Count, "A").End(xlUp).Row
        LastCol = Cells(1, Columns.Count).End(xlToLeft).Column
    End With
    Range("A1", Cells(LastRow, LastCol)).Select
    Selection.Name = "tbl_P"
'
    thiswksht.Sort.SortFields.Clear
    thiswksht.Sort.SortFields.Add Key:=Range("A1", Cells(LastRow, LastCol)), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
    With thiswksht.Sort
        .SetRange Range("A1", Cells(LastRow, LastCol))
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    Range("A1").Select
End Sub
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Try
Code:
thiswksht.Sort.SortFields.Add Key:=Range("A2:A" & LastRow), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
 
Upvote 0
Thank you sir! I had tried "A2" LastRow LastCol without a header row, but obviously not the solution you provided.
Is there a simple explanation for why the "key" range needs to be changed to exclude the first cell of the sort range?

Ron
 
Upvote 0
You can include the first cell, but it has to be a single column per field , rather then the complete range.
 
Upvote 0
You can include the first cell, but it has to be a single column per field , rather then the complete range.

Thanks again! Working to get my head around "field," I think, gives me the relationship between "key" and "field." While both are optional, when "key" is set as a Range, the range can't contain the header, only the values to be sorted.

NameRequired/OptionalData TypeDescription
Key1OptionalVariantSpecifies the first sort field, either as a range name (String) or Range object; determines the values to be sorted.

<thead style="box-sizing: border-box;">
</thead><tbody style="box-sizing: border-box;">
</tbody>

NameRequired/OptionalData TypeDescription
FieldOptionalVariantThe integer offset of the field on which you want to base the filter (from the left of the list; the leftmost field is field one).

<thead style="box-sizing: border-box;">
</thead><tbody style="box-sizing: border-box;">
</tbody>
 
Upvote 0

Forum statistics

Threads
1,214,932
Messages
6,122,331
Members
449,077
Latest member
jmsotelo

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