Why is this code to sort data falling over?

TheWennerWoman

Active Member
Joined
Aug 1, 2019
Messages
270
Office Version
  1. 365
Platform
  1. Windows
Hope someone can help, I'm really puzzled by this. Basically, I am looking to sort a dataset based on column AY (smallest to largest). This is my code:
Code:
Columns("A:DY").Select
Range("DY1").Activate
ActiveWorkbook.Worksheets("7c. Consolidated Tagetik Input").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("7c. Consolidated Tagetik Input").Sort.SortFields.Add2 Key:=Range("DY2:DY" & lastrow), SortOn:=xlSortOnValues, Order:=xlAscending _
, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("7c. Consolidated Tagetik Input").Sort
    .SetRange Range("A1:DY" & lastrow)
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With

The variable lastrow is declared just fine but this falls over with
Code:
Run time error 438: object doesn't support this property or method

I think I might even have used the macro recorder to get the code in the first place so I'm totally confused!
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Are you trying to sort by col AY or DY?
 
Upvote 0
Ok, how about
VBA Code:
With Worksheets("7c. Consolidated Tagetik Input")
   .Range("A1:DY" & .Range("A" & Rows.Count).End(xlUp)).Sort key1:=.Range("DY1"), Order1:=xlAscending, Header:=xlYes
End With
 
Upvote 0
Solution
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,059
Messages
6,122,918
Members
449,094
Latest member
teemeren

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