View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.programming
Bob Phillips[_6_] Bob Phillips[_6_] is offline
external usenet poster
 
Posts: 11,272
Default Can I use a variable as a sort key in VB?

Dim mynumber
mynumber = InputBox("Please enter the column number that you want the data
splitting by")
Range("A1").Select
ActiveCell.CurrentRegion.Select
Selection.Sort Key1:=Selection.Cells(1, CLng(mynumber)),
Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom

--

HTH

RP
(remove nothere from the email address if mailing direct)


"Pablo Bellissimo" <Pablo wrote in
message ...
Hi,

I am trying to write a piece of code which will present users with an

input
box (asking for a number) which will then select the current range and

sort
it using the variable defined by the input box. How would I use the

numeric
variable as the key to sorting my data.

Example:

mynumber = InputBox("Please enter the column number that you want the data
splitting by")
Range("A1").Select
ActiveCell.CurrentRegion.Select
Selection.Sort Key1:=mynumber, Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom

The problem must be around my storing a number as the variable but the

sort
command would appear to want a string???

Please help...