Sort VBA Question

heffo500

New Member
Joined
Sep 28, 2016
Messages
44
Hi

When I run the below I get an error, it only seems to happen when there is only one row to sort:

lr2 = Range("D" & Rows.Count).End(xlUp).Row
Range("D1:N" & lr2).Select
ActiveWorkbook.Worksheets("Data").Sort.SortFields.Add2 Key:=Range("N1:N" & lr2), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Data").Sort
.SetRange Range("D1:N" & lr2)
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With

Does it fall over because I only have one row to sort?
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
What error message and what line of code?

Try putting :
Code:
ActiveWorkbook.Worksheets("Data").Sort.SortFields.Clear

After :
Code:
Range("D1:N" & lr2).Select
 
Upvote 0
Thanks for the reply the error appears at .Apply

The error is:

Run-time error '1004':

The sort reference is not valid. Make sure that it's within the data you want to sort, and the first sort by box isn't the same or blank.
 
Upvote 0
Try changing to :
Code:
Worksheets("Data").Sort.SortFields.Add2 Key:=Worksheets("Data").Range("N1:N" & lr2), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal

Make sure the "Data" sheet is the active sheet.
 
Last edited:
Upvote 0
Try changing to :
Code:
Worksheets("Data").Sort.SortFields.Add2 Key:=Worksheets("Data").Range("N1:N" & lr2), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal

Make sure the "Data" sheet is the active sheet.


I see to get a different issue in my code first:

Sheets("
Sheet1
").Select

lr = Range("A" & Rows.Count).End(xlUp).Row
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("E1:E" & lr), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
With ActiveWorkbook.Worksheets("
Sheet1
").Sort
.SetRange Range("A1:R" & lr)
.Header = xlNo
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With

ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("E1:E" & lr), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal

This line seems to have the issue with Run time error '1004': Application-defined or object-defined error.

When I run the macro it works but after about the third time running this error appears.
 
Upvote 0
Did you try making the changes per posts #'s 2 and 4 ?

If still not working, I don't know - perhaps someone can help.
 
Last edited:
Upvote 0
thanks for help, after restarting the PC the errors seemed to disappear. I'm testing a lot to try repeat the error but it hasn't reoccured
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,289
Members
452,631
Latest member
a_potato

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