PDA

View Full Version : Solved: Sort specific workbook



gscarter
07-23-2008, 08:32 AM
I recorded the macro:




Function sort(column1 As String, column2 As String)

construct = column1 & ":" & column2

Columns(construct).Select
Selection.sort Key1:=Range(column1 & "1"), Order1:=xlAscending, Key2:=Range(column2 & "1") _
, Order2:=xlDescending, Header:=xlGuess, OrderCustom:=1, MatchCase:= _
False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal, DataOption2 _
:=xlSortNormal

End Function


to sort different columns in my workbook dependent upon which columns i pass into it.

So if i were to call:



sort "A" "B"

It would sort columns A and B.

During the sub which this is included in, other workbooks are opened so information can be copied from them.

The problem i am having is this macro is being applied to the workbook which is most recently opened rather than the one i am copying data to.

Does anyone know how i can adapt the code to be applied to a specific workbook rather than the one that is opened?

Thanks
Gary

RonMcK
07-23-2008, 09:01 AM
Gary,

Place this somewhere before you do the sort: Workbooks(SourceFile).Activate Replace 'SourceFile' with "Your Filename.xls" (in quotes).

HTH,

gscarter
07-23-2008, 09:16 AM
Thanks HTH,

Worked a treat.