Logo 
Search:

MS Office Forum

Ask Question   UnAnswered
Home » Forum » MS Office       RSS Feeds

Sort Problems

  Asked By: Chisisi    Date: Mar 10    Category: MS Office    Views: 751
  

I'm running this code, but i always get a runtime error. Sort
Method or Range class failed.. Please tell me whats wrong with it..


If cbSortMonth.Value = "(ALL)" And obOrder = True And cbOrder.Value
= "Ascending" Then
ThisWorkbook.Worksheets("GC Report").Rows("8:50").
ThisWorkbook.Worksheets("GC Report").Rows("8:50").sort
key1:=ThisWorkbook.Worksheets("GC Report").Range("AL8"),
Order1:=xlAscending, Header:=xlNo
End If


i am running this on a worksheet via a command button.. please help..

Share: 

 

7 Answers Found

 
Answer #1    Answered By: Rudy Turner     Answered On: Mar 10

You seem to have the following code

ThisWorkbook.Worksheets("GC Report").Rows("8:50").

 
Answer #2    Answered By: Zivah Levi     Answered On: Mar 10

Thanks for the reply, but, No sir. I just paste it the wrong  way.. sorry for
that.. well i have this new code, but still it doesnt work. Still a run time
error.
Please help  me out..

Dim w As Worksheet
Set w = ThisWorkbook.Worksheets("GC Report")

If cbSortMonth.Value = "(ALL)" And obOrder = True And cbOrder.Value =
"Ascending" Then
w.Rows("8:50").sort key1:=w.Range("AL8"), Order1:=xlAscending
End If

What's wrong with it?

 
Answer #3    Answered By: Shayne Anderson     Answered On: Mar 10

There seems to be nothing wrong  with the code. Are there any locked cells in
the worksheet  or is it protected? If so you will have to unprotect it to run
a sort.

 
Answer #4    Answered By: Ludano Ricci     Answered On: Mar 10

What's the specific error  and error number you get?

I'm suspicious of the Range("AL8") myself. Do you have data in column AL?

Are you using Excel 2007?

Are you sure the error is occurring on the Sort method and not on the prior
If statement?

Are you sure that w is correctly being assigned its value?

It's only two lines of code, you can test every single expression in it.

You can go into the Immediate Window, and say ?w.Name,
?w.rows("8:50").Count, etc. to validate each expression.

 
Answer #5    Answered By: Shannon Freeman     Answered On: Mar 10

I think i know what my problem is, my sheet is protected! ahaha! i forget to
unlock before running  the code.. sorry for my stupidity.. and thanks for the
replies

 
Answer #6    Answered By: Hubayshah Mansour     Answered On: Mar 10

Hopefully your problem was with the sheet protection. But I'd already
written this message, so, just in case. ...

I have just tried this code

Option Explicit

Private Sub CommandButton1_Click()
Dim w As Worksheet
Set w = ThisWorkbook.Worksheets("Sheet1")

'If cbSortMonth.Value = "(ALL)" And obOrder = True And cbOrder.Value =
"Ascending" Then
w.Rows("8:50").Sort key1:=w.Range("AL8"), Order1:=xlAscending
'End If

End Sub

which is pretty much a copy of yours, but with the IF statement commented
out.

It runs fine.

Which line does yours error  on in the code  you now have? And what
specifically is the run-time error now?

I wonder if it is the IF statement that is failing. Do you have Option
Explicit in your code? If not, please put it in and try it again.

What specific structures are cbSortMonth, obOrder and cbOrder? CB would
normally be a check box, but that doesn't look right here. OB? It's not a
typo is it?

 
Answer #7    Answered By: Dallas Martin     Answered On: Mar 10

but it is really the sheet protection..
But thanks for the response.
I wonder, what does options explicit do?

 
Didn't find what you were looking for? Find more on Sort Problems Or get search suggestion and latest updates.




Tagged: