ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Sorting From Macro in another workbook (https://www.excelbanter.com/excel-programming/310084-sorting-macro-another-workbook.html)

Rick

Sorting From Macro in another workbook
 
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(xlU p).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

Tom Ogilvy

Sorting From Macro in another workbook
 
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

--
Regards,
Tom Ogilvy



"Rick" wrote in message
...
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(xlU p).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





All times are GMT +1. The time now is 12:09 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com