ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   SORT: Is it possible to supply operands via function / vba sub? (https://www.excelbanter.com/excel-programming/391563-sort-possible-supply-operands-via-function-vba-sub.html)

Mike Iacovou

SORT: Is it possible to supply operands via function / vba sub?
 
Hi all,

I am writing a Sub to process a CSV. This code will perform (amongst other
bits) a SORT on the data. However, the sort procedure will vary with the data
- such that the sort Range, Order & Key will vary.
I was hoping it would be possible to supply the range,order & key TO the
Sub, and then use the passed information within the SORT vba code to perform
the function... but I can't find any information regarding declaring the
Order operands etc... can this be done?

I know that the Key can be specified by referencing a cell, but can I just
use an expression directly? What is Order object / operand to be declared as
etc?

Any guidance greatly appreciated. KR

Mike

joel

SORT: Is it possible to supply operands via function / vba sub?
 
sub mysort(target as range,mykey1 as integer, mykey2 as integer, _
mykey3 as integer)

Worksheets("sheet1"). _
Range(target), Cells(MaxRows, 10)).Sort _
Key1:=Worksheets("sheet1").Columns(mykey1), _
Key2:=Worksheets("sheet1").Columns(mykey2), _
Key3:=Worksheets("sheet1").Columns(mykey3), _
Header:=xlNo, DataOption1:=xlSortNormal

sort order uses ther following constant
Const xlAscending = 1
Const xlDescending = 2


"Mike Iacovou" wrote:

Hi all,

I am writing a Sub to process a CSV. This code will perform (amongst other
bits) a SORT on the data. However, the sort procedure will vary with the data
- such that the sort Range, Order & Key will vary.
I was hoping it would be possible to supply the range,order & key TO the
Sub, and then use the passed information within the SORT vba code to perform
the function... but I can't find any information regarding declaring the
Order operands etc... can this be done?

I know that the Key can be specified by referencing a cell, but can I just
use an expression directly? What is Order object / operand to be declared as
etc?

Any guidance greatly appreciated. KR

Mike


Mike Iacovou

SORT: Is it possible to supply operands via function / vba sub
 
thanks joel

i'll give that a try. cheers.

Mike

joel

SORT: Is it possible to supply operands via function / vba sub
 
I got the range wrong. Below is the correction.

Worksheets("sheet1"). _
Range(target).Sort _
Key1:=Worksheets("sheet1").Columns(mykey1), _
Key2:=Worksheets("sheet1").Columns(mykey2), _
Key3:=Worksheets("sheet1").Columns(mykey3), _
Header:=xlNo, DataOption1:=xlSortNormal


"Mike Iacovou" wrote:

thanks joel

i'll give that a try. cheers.

Mike



All times are GMT +1. The time now is 05:46 PM.

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