Sort VBA

vmjan02

Well-known Member
Joined
Aug 15, 2012
Messages
1,062
Office Version
  1. 365
  2. 2021
  3. 2019
  4. 2016
  5. 2013
I have the below code, what it does it selects the columns (A to N) then till the last non blank sell and then it will sort column D (Contains date) order1:=xlAscending, Header:=xlNo

but giving me an error as "The sort data is not valid, make sure that its within the data you want to sort"

Code:
Sub Sot()
ActiveSheet.Range("A1").Select
ActiveSheet.Range(Selection, Selection.End(xlToRight)).Select
ActiveSheet.Range(Selection, Selection.End(xlDown)).Select
Selection.sort key1:=Range("D1", Range("D1").End(xlDown)), _
order1:=xlAscending, Header:=xlNo
end sub
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Just use Range("D1") for the key.
 
Upvote 0
I guess your code is in another worksheet then? Use ActiveSheet.Range("D1")
 
Upvote 0
I'd suggest this:

Code:
Sub Sot()
ActiveSheet.Range("A1").Currentregion.sort key1:=ActiveSheet.Range("D1"), _
order1:=xlAscending, Header:=xlNo
end sub

If that still gives an error, please state the error message.
 
Upvote 0

Forum statistics

Threads
1,215,656
Messages
6,126,055
Members
449,284
Latest member
fULMIEX

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