Macro Sort

MyCuteHeart

New Member
Joined
Mar 7, 2012
Messages
6
Hi Guyz,

Been recording a macro that sorts different columns at a time but after recording it and tried the macro, it always give me an error and does not do the sorthing. See below part of the code where it gives an error. Please help. Thanks a lot.

Text in bold is the one making the error.

Range("D2:D31").Select
ActiveWorkbook.Worksheets("jag2_ptr_thermal prt rev10 03_0").Sort.SortFields. _
Clear

ActiveWorkbook.Worksheets("jag2_ptr_thermal prt rev10 03_0").Sort.SortFields. _
Add Key:=Range("D2:D31"), SortOn:=xlSortOnValues, Order:=xlAscending, _
DataOption:=xlSortNormal
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying

jproffer

Well-known Member
Joined
Dec 15, 2004
Messages
2,647
It should work without it. You're setting new sortfields in the next line.
 

MyCuteHeart

New Member
Joined
Mar 7, 2012
Messages
6
Hi Jproffer,

The macro was recorded that way. I tried erasing this line, then the next line was the next error. :(
 

Smitty

Legend
Joined
May 15, 2003
Messages
29,536
Welcome to the Board!

Is the worksheet protected?

What error do you get?
 

MyCuteHeart

New Member
Joined
Mar 7, 2012
Messages
6

ADVERTISEMENT

Hi Smith,

The worksheet is not protected. And I get an error like this.

Run-time error '1004':
Application-defined or object-defined error

then when I clik debug, it points to the code in bold letters above.
Please help. Thanks
 

jproffer

Well-known Member
Joined
Dec 15, 2004
Messages
2,647
I ran it on a sheet with that name and it ran through....it didn't DO anything, but it didn't error. Are you sure the sheet name is correct?...Object error sounds like it's something like that.
 

MyCuteHeart

New Member
Joined
Mar 7, 2012
Messages
6

ADVERTISEMENT

I ran it on a sheet with that name and it ran through....it didn't DO anything, but it didn't error. Are you sure the sheet name is correct?...Object error sounds like it's something like that.
Hi jproffer,

How to know if the sheet has a name as that. Sorry I'm new in macro.
What I do is, I record the macro in an excel file and then I export the macro and I import the said macro to another excel file and try to run it. Then it gives me an error. What should I do? Thanks.
 

jproffer

Well-known Member
Joined
Dec 15, 2004
Messages
2,647
The name on the tab should match the code exactly or it won't run.

Do you mean you record it in one workbook, then move it to a second workbook?
 

MyCuteHeart

New Member
Joined
Mar 7, 2012
Messages
6
The name on the tab should match the code exactly or it won't run.

Do you mean you record it in one workbook, then move it to a second workbook?
Hi Jproffer,

Yah. I got it now. Thank you very much Sir. Hope to help me again if I need a help. Thanks again.
 

Forum statistics

Threads
1,137,156
Messages
5,679,924
Members
419,862
Latest member
Bluewings666

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
Top