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

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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
 
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
 
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
 

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

Back
Top