Sorting From Macro in another workbook

G

Guest

I have code in a workbook1 which opens workbook2, does some trivial items,
then selects the range A:K in workbook 2. I am then trying to sort the range
in workbook2 from the code in workbook1, but am getting the following error:

"Run time error '1004': Application defined or object defined error."

Following is the select code. Can someone please tell me what is causing
the error and how to correct? Thanks.

Workbooks.Open Filename:="P:\PRT\IndustryReview\Client Summary
Performance.xls"
Windows("Client Summary Performance.xls").Activate
ActiveWorkbook.ActiveSheet.Range("A65536").End(xlUp).Offset(1, 0).Select
Selection.PasteSpecial Paste:=xlPasteValues
Selection.PasteSpecial Paste:=xlPasteFormats
Columns("A:K").Select
Selection.Sort Key1:=Range("A2"), Order1:=xlAscending, Key2:=Range("B2") _
, Order2:=xlDescending, Header:=xlGuess, OrderCustom:=1, MatchCase:= _
False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal,
DataOption2 _
:=xlSortNormal
 
T

Tom Ogilvy

Where is the code located. Is it in a code module for a sheet - possibly
being run from a commandbutton.

if so try something like this:

Dim bk as Workbooks
Dim rng as Range

set bk =Workbooks.Open( Filename:= _
"P:\PRT\IndustryReview\Client Summary Performance.xls"
Windows("Client Summary Performance.xls").Activate
set rng = bk.ActiveSheet.Range("A65536").End(xlUp).Offset(1, 0)
rng.PasteSpecial Paste:=xlPasteValues
rng.PasteSpecial Paste:=xlPasteFormats
rng.parent.Columns("A:K").Sort _
Key1:=rng.parent.Range("A2"), Order1:=xlAscending, _
Key2:=rng.parent.Range("B2"), Order2:=xlDescending, _
Header:=xlGuess, OrderCustom:=1, MatchCase:=False, _
Orientation:=xlTopToBottom, DataOption1:=xlSortNormal, _
DataOption2:=xlSortNormal
 

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

Top