Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Sorting a Range using VBA


Assume that I have a variable "NumCount" and I want to sort the range of
cells from (1,B) to (2,NumCount), sort by the first column accending
order. What kind of code could I use ?


--
SystemHack
------------------------------------------------------------------------
SystemHack's Profile: http://www.excelforum.com/member.php...o&userid=26614
View this thread: http://www.excelforum.com/showthread...hreadid=399388

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 923
Default Sorting a Range using VBA


The following is literally what you asked .....

Range(Cells(1, "B"), Cells(2, NumCount)).Sort Key1:=Cells(1, NumCount)

but be careful in specifying the range sort keys. You can also use the
number of the column eg 2 not "B" as you asked.
also note that when defining a range using Cells the first value is the row
number, the second the column.
--
Cheers
Nigel



"SystemHack" wrote
in message ...

Assume that I have a variable "NumCount" and I want to sort the range of
cells from (1,B) to (2,NumCount), sort by the first column accending
order. What kind of code could I use ?


--
SystemHack
------------------------------------------------------------------------
SystemHack's Profile:

http://www.excelforum.com/member.php...o&userid=26614
View this thread: http://www.excelforum.com/showthread...hreadid=399388



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Sorting a Range using VBA


Thanks a ton. The problem was I didn't know you could embed the Cells
command into a Range function. That made everyting sooo much easier.
:) Exactly what I as looking for thanks !


--
SystemHack
------------------------------------------------------------------------
SystemHack's Profile: http://www.excelforum.com/member.php...o&userid=26614
View this thread: http://www.excelforum.com/showthread...hreadid=399388

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Sorting a Range using VBA


Ok I can get this to work on the same page but not on a different sheet.
This is the code I am trying to use.


Private Sub CommandButton1_Click()

Sheets(2).Range(Cells(1, 2), Cells(12, 2)).Sort Key1:=Cells(1, 2)

End Sub

Any ideas why this wouldn't work ?


--
SystemHack
------------------------------------------------------------------------
SystemHack's Profile: http://www.excelforum.com/member.php...o&userid=26614
View this thread: http://www.excelforum.com/showthread...hreadid=399388

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 923
Default Sorting a Range using VBA

the sort key must be defined in context as well.

Sheets(2).Range(Cells(1, 2), Cells(12, 2)).Sort Key1:=Sheet(2).Cells(1, 2)

or

With Sheet(2)
.Range(Cells(1,2),Cells(12,2)).Sort Key1:=.Cells(1,2)
End With
--
Cheers
Nigel



"SystemHack" wrote
in message ...

Ok I can get this to work on the same page but not on a different sheet.
This is the code I am trying to use.


Private Sub CommandButton1_Click()

Sheets(2).Range(Cells(1, 2), Cells(12, 2)).Sort Key1:=Cells(1, 2)

End Sub

Any ideas why this wouldn't work ?


--
SystemHack
------------------------------------------------------------------------
SystemHack's Profile:

http://www.excelforum.com/member.php...o&userid=26614
View this thread: http://www.excelforum.com/showthread...hreadid=399388





  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 923
Default Sorting a Range using VBA

sorry meant to use Sheets not Sheet !!

--
Cheers
Nigel



"Nigel" wrote in message
...
the sort key must be defined in context as well.

Sheets(2).Range(Cells(1, 2), Cells(12, 2)).Sort Key1:=Sheet(2).Cells(1, 2)

or

With Sheet(2)
.Range(Cells(1,2),Cells(12,2)).Sort Key1:=.Cells(1,2)
End With
--
Cheers
Nigel



"SystemHack"

wrote
in message ...

Ok I can get this to work on the same page but not on a different sheet.
This is the code I am trying to use.


Private Sub CommandButton1_Click()

Sheets(2).Range(Cells(1, 2), Cells(12, 2)).Sort Key1:=Cells(1, 2)

End Sub

Any ideas why this wouldn't work ?


--
SystemHack
------------------------------------------------------------------------
SystemHack's Profile:

http://www.excelforum.com/member.php...o&userid=26614
View this thread:

http://www.excelforum.com/showthread...hreadid=399388





  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 318
Default Sorting a Range using VBA

Is it preferable to change the other Cells to .Cells as well?

With Sheet(2)
.Range(.Cells(1,2),.Cells(12,2)).Sort Key1:=.Cells(1,2)
End With

Alok

"Nigel" wrote:

the sort key must be defined in context as well.

Sheets(2).Range(Cells(1, 2), Cells(12, 2)).Sort Key1:=Sheet(2).Cells(1, 2)

or

With Sheet(2)
.Range(Cells(1,2),Cells(12,2)).Sort Key1:=.Cells(1,2)
End With
--
Cheers
Nigel



"SystemHack" wrote
in message ...

Ok I can get this to work on the same page but not on a different sheet.
This is the code I am trying to use.


Private Sub CommandButton1_Click()

Sheets(2).Range(Cells(1, 2), Cells(12, 2)).Sort Key1:=Cells(1, 2)

End Sub

Any ideas why this wouldn't work ?


--
SystemHack
------------------------------------------------------------------------
SystemHack's Profile:

http://www.excelforum.com/member.php...o&userid=26614
View this thread: http://www.excelforum.com/showthread...hreadid=399388




  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,092
Default Sorting a Range using VBA

Dim NumCount As Long

NumCount = Cells(Rows.Count, "A").End(xlUp).Row
Range("A1:B" & NumCount).Sort Key1:=Range("A1")
End Sub

Mike F
"SystemHack" wrote
in message ...

Assume that I have a variable "NumCount" and I want to sort the range of
cells from (1,B) to (2,NumCount), sort by the first column accending
order. What kind of code could I use ?


--
SystemHack
------------------------------------------------------------------------
SystemHack's Profile:
http://www.excelforum.com/member.php...o&userid=26614
View this thread: http://www.excelforum.com/showthread...hreadid=399388



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
sorting range Jase Excel Discussion (Misc queries) 1 September 30th 08 06:42 PM
sorting range peyman Excel Discussion (Misc queries) 5 October 27th 07 10:29 PM
Sorting by range planetdust New Users to Excel 3 October 21st 07 01:03 AM
Sorting a Range cmm Excel Discussion (Misc queries) 1 June 30th 06 01:35 PM
sorting a range cjsmith22 Excel Discussion (Misc queries) 2 November 13th 05 11:19 PM


All times are GMT +1. The time now is 09:42 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"