Application-defined or object-defined error on sort command

raeannb

Board Regular
Joined
Jun 21, 2011
Messages
86
Hi! I have the following line of code that keeps giving me a "Run-time error '1004': Application-defined or object-defined error," and I can't seem to figure out why:

.Range(.Cells(7, 1), .Cells(x, lastcol)).Sort Key:=Columns(mycol), Order:=xlAscending, Header:=xlYes

My table extends from cell A7 to row x, column "lastcol," both of which are being found correctly (I checked). The line is preceded by a With statement selecting the worksheet I want to use. The column I'd like to sort by is
"mycol," another integer being found correctly. I've never embedded a .Cells statement in a Range statement before, but I read online that it should work, sooo....what the heck am I doing wrong?

Thank you!!
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
I think you also need to declare the sheet on the Key...
And both key and order need to be numbered (key1 order1)

Try

.Range(.Cells(7, 1), .Cells(x, lc)).Sort key1:=.Columns(mycol), Order1:=xlAscending, Header:=xlYes
 
Upvote 0
is 1 <= mycol <= lastcol ?
have you tried Key1:=.Cells(7, mycol)

What With...End With is this inside?

Perhaps removing the dot from Range.

Code:
With someThing
    Range(.Cells(7, 1), .Cells(x, lastcol)).Sort Key1:=.Cells(1, mycol), Order1:=xlAscending, Header:=xlYes
End With
 
Upvote 0
I think you also need to declare the sheet on the Key...
And both key and order need to be numbered (key1 order1)

Try

.Range(.Cells(7, 1), .Cells(x, lc)).Sort key1:=.Columns(mycol), Order1:=xlAscending, Header:=xlYes


It worked!!! As dumb as I feel, I love quick fixes...thank you!!
 
Upvote 0
It worked for me too, but in one of my earlier codes, I was able to use it without key1 and it still worked...
idk what I did there.
 
Upvote 0

Forum statistics

Threads
1,224,603
Messages
6,179,853
Members
452,948
Latest member
UsmanAli786

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