Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 208
Default Sort Selected Cell References

Hi

Does anyone know how to sort a set of cell references (not the values
in the cells, but the cell references - A1, B5...)

I am writing a macro where the user can select any number of cells in
any order, but when I process the macro, I need to start with the
upper left most cell in the selection.

Any help would be greatly appreciated!
Thanks
Tim
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,501
Default Sort Selected Cell References

Hi,

I started by thinking this was simple and had never recognised that Excel
remembered a selection of cells in the order it was selected. there has got
to be a simpler way but here goes. This uses columns A,B & C on the active
sheet for the sort but parctically you would have to put that out of the way
somewhere. But to test it avoid using those columns in your selection. Right
click the sheet tab, view code and paste this in. make you random selection
of cells and run it

Sub stantial()
Dim bigrange As Range, myrange As Range
x = 1
Set myrange = Selection
For Each c In myrange
Cells(x, 1).Value = c.Address
Cells(x, 2).Value = c.Row
Cells(x, 3).Value = c.Column
x = x + 1
Next
lastrow = Cells(Rows.Count, "A").End(xlUp).Row
Set myrange = Range("A1:A" & lastrow).Resize(, 3)
myrange.Select
Selection.Sort Key1:=Range("B1"), Order1:=xlAscending, Key2:=Range("C1"),
Order2:=xlAscending
Set myrange = Range("A2:A" & lastrow)
Set bigrange = Range(Range("A1").Text)
For Each c In myrange
Set bigrange = Union(bigrange, Range(c.Text))
Next
'this is for debugging only
'to demonstrate cells are in order
For Each c In bigrange
MsgBox c.Address
Next
End Sub

Mike

"Tim879" wrote:

Hi

Does anyone know how to sort a set of cell references (not the values
in the cells, but the cell references - A1, B5...)

I am writing a macro where the user can select any number of cells in
any order, but when I process the macro, I need to start with the
upper left most cell in the selection.

Any help would be greatly appreciated!
Thanks
Tim

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,073
Default Sort Selected Cell References

On Jun 12, 5:54 am, Tim879 wrote:
Hi

Does anyone know how to sort a set of cell references (not the values
in the cells, but the cell references - A1, B5...)

I am writing a macro where the user can select any number of cells in
any order, but when I process the macro, I need to start with the
upper left most cell in the selection.

Any help would be greatly appreciated!
Thanks
Tim


If your range of cells from which the selection is made is fixed (I've
restricted it to the 100 cells in A1:J10), then maybe this will be
useful...

Public Sub OrderSelectedCells()
Dim rgCell As Range, I As Long
For Each rgCell In Range("A1:J10")
If Not Intersect(Selection, rgCell) Is Nothing Then
Debug.Print rgCell.Address(False, False)
End If
Next
End Sub

It prints a cell's address to the Immediate window if it was selected.
The sequence of the testing for selection, and hence printing order,
is top row to bottom row and left to right.

Perhaps you could substitute Range("A1:J10") with UsedRange.

Ken Johnson
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 208
Default Sort Selected Cell References

thank you both for your help. I'll play around with the code you've
provided and post what I come up with.


Jun 11, 8:14 pm, Ken Johnson wrote:
On Jun 12, 5:54 am, Tim879 wrote:

Hi


Does anyone know how to sort a set of cell references (not the values
in the cells, but the cell references - A1, B5...)


I am writing a macro where the user can select any number of cells in
any order, but when I process the macro, I need to start with the
upper left most cell in the selection.


Any help would be greatly appreciated!
Thanks
Tim


If your range of cells from which the selection is made is fixed (I've
restricted it to the 100 cells in A1:J10), then maybe this will be
useful...

Public Sub OrderSelectedCells()
Dim rgCell As Range, I As Long
For Each rgCell In Range("A1:J10")
If Not Intersect(Selection, rgCell) Is Nothing Then
Debug.Print rgCell.Address(False, False)
End If
Next
End Sub

It prints a cell's address to the Immediate window if it was selected.
The sequence of the testing for selection, and hence printing order,
is top row to bottom row and left to right.

Perhaps you could substitute Range("A1:J10") with UsedRange.

Ken Johnson


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
When I sort how do I keep cell references correct? GLS Excel Discussion (Misc queries) 4 February 6th 11 08:46 PM
how do i sort rows and maintain cell references tfb Excel Worksheet Functions 1 October 8th 06 06:54 PM
how do i sort rows and maintain cell references tfb Excel Worksheet Functions 0 October 8th 06 05:43 PM
Can I sort Excel sheet by the color of the fill in selected cell hg Excel Worksheet Functions 1 March 14th 06 02:37 AM
covert only cell references in selected cells into value leaving the existingcell formula al007 Excel Programming 16 January 21st 06 02:29 AM


All times are GMT +1. The time now is 07:15 AM.

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

About Us

"It's about Microsoft Excel"