Macro to Sort Data

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,561
Office Version
  1. 2021
Platform
  1. Windows
I have writen code to sort data in Descending order on sheets "Overaged Inventory"

However when running the macro, I get a message, "The sort reference is not valuid.... "

The data to be sorted in in Col I


It would be appreciated if someone could kindly amend my code

Code:
 Sub Sort_Ageing_Overaged()
 Dim Lr As Long
 With Sheets("Overaged inventory")
Lr = Cells(.Rows.Count, "A").End(xlUp).Row
.Range("a2:K" & Lr).Sort Key1:=Range("I1" & Lr), Order1:=xlDescending
  End With
End Sub
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Thanks for the help. I made the change. When I run the macro from the sheet "overaged Inventory". the macro runs perfectly.

When I run from another sheet, I get same run time error


The problem was that I did not have a . before range("{i1"),order1

It shoudl be
Code:
 .Range("a2:K" & Lr).Sort Key1:=.Range("I1"), Order1:=xlDescending
 
Upvote 0
The problem was that I did not have a . before range("{i1"),order1
That fixes one problem but you could easily have another. It is the same issue except this will not throw an error but may not sort your data correctly. This line of code is also missing a "."

Rich (BB code):
Lr = Cells(.Rows.Count, "A").End(xlUp).Row
It should be
Rich (BB code):
Lr = .Cells(.Rows.Count, "A").End(xlUp).Row

The first line above will calculate the last row based on column A of the active sheet, not "Overaged inventory"
So if the active sheet had say 5 less rows in column A compared to "Overaged inventory" then the last 5 rows in "Overaged inventory" will not get sorted.

BTW, you will find code easier to understand, debug etc if you align blocks of the code better. I have mentioned this to you several times before. One example: Macro to remove leading spaces in Col A

VBA Code:
Sub Sort_Ageing_Overaged()
  Dim Lr As Long
  With Sheets("Overaged inventory")
    Lr = .Cells(.Rows.Count, "A").End(xlUp).Row
    .Range("a2:K" & Lr).Sort Key1:=.Range("I1"), Order1:=xlDescending
  End With
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,214,827
Messages
6,121,824
Members
449,050
Latest member
Bradel

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