Question about sorting and "key" range values

T

TBA

I have a simple sort I wish VBA to accomplish. The code looks something
like this:

Range("Database").Select
Selection.Sort Key1:=Range("???"), Order1:=xlAscending, _
Header:=xlGuess, OrderCustom:=1, MatchCase:=False, _
Orientation:=xlTopToBottom

Range("A1").Select

The only problem is that my Key1 range [Key1:=Range("???")] will not always
be in the same column depending on what table I'm working with. However, I
do have a variable SortColumn (an integer) that indicates what column my
sort key is in, and I also have a variable SortHeader (string) that
indicates what column header I wish to sort by. SortColumn will always be
the column number that SortHeader is in, obtained through another bit of
simple code. Can either of these variables be used in place of the ??? in
the key range? At the moment I don't have access to Excel, otherwise I'd
test this myself. Does this make any sense?

All suggestions welcome. Thanks!

-gk-
 
T

Tom Ogilvy

Range("Database").Select
Selection.Sort Key1:=Cells(selection.Row,SortColumn), _
Order1:=xlAscending, _
Header:=xlGuess, OrderCustom:=1, MatchCase:=False, _
Orientation:=xlTopToBottom

assume sortcolumn is the absolute column number (J would be 10 for
instance).
 
D

Dave Peterson

SortColumn varies between 1 and 255. It's the actual column number?

key1:=cells(1,sortcolumn)

might work ok.

(excel is very forgiving, but that column better intersect with the range to get
sorted someplace.)

I have a simple sort I wish VBA to accomplish. The code looks something
like this:

Range("Database").Select
Selection.Sort Key1:=Range("???"), Order1:=xlAscending, _
Header:=xlGuess, OrderCustom:=1, MatchCase:=False, _
Orientation:=xlTopToBottom

Range("A1").Select

The only problem is that my Key1 range [Key1:=Range("???")] will not always
be in the same column depending on what table I'm working with. However, I
do have a variable SortColumn (an integer) that indicates what column my
sort key is in, and I also have a variable SortHeader (string) that
indicates what column header I wish to sort by. SortColumn will always be
the column number that SortHeader is in, obtained through another bit of
simple code. Can either of these variables be used in place of the ??? in
the key range? At the moment I don't have access to Excel, otherwise I'd
test this myself. Does this make any sense?

All suggestions welcome. Thanks!

-gk-
 

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