Inconsistent run-time error

  • Thread starter Thread starter William Bartusek
  • Start date Start date
W

William Bartusek

Why does the following block of code work when using a
macro button on a custom toolbar but not work when using a
control button on a worksheet??? I get a run-time error
code 1004, Method 'range of object_Worksheet' failed
pointing to the Sort block of code.

Private Sub NewScenario_Click()

'sort the "Worksheet_Name" range by Ascending order of
Scenario
Application.Worksheets("Lookup_Ranges").Activate
Application.Worksheets("Lookup_Ranges").Range
("Worksheet_Name").Activate

'sort Worksheet names by column 2 of "Worksheet_Name" range

Application.Worksheets("Lookup_Ranges").Range
("Worksheet_Name").Sort _
Key1:=Range("Worksheet_Name").Columns(2), _
Order1:=xlAscending, Header:=xlNo

'open "SetBaseline" userform
SetBaseline.Show
SetBaseline.Baseline.SetFocus

End Sub
 
Are you using xl97?

There's a bug in xl97 (fixed in xl2k) that deals with code called from controls
from the control toolbox toolbar that are used on a worksheet.

One fix is to change the .takefocusonclick property to false.

Another fix is to add:
activecell.activate
at the top of your code.

===
If you're not using xl97, maybe you could eliminate some of the
selecting/activating:

With Worksheets("lookup_ranges").Range("worksheet_name")
.Cells.Sort Key1:=.Columns(2), _
Order1:=xlAscending, Header:=xlNo
End With

Untested, but compiled.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

VBA Sort Problem 2
Sort all worksheets in a workbook 3
sorting range 5
Run-time error 1004... 4
Sort multiple columns on all worksheets 2
Is not sorting 4
toggle sort columns 5
MACRO Run Time Error 1004 1

Back
Top