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

I have xl2002. In in VBA. I have a vector (or one column matrix) of short
strings. I'd like to sort them. Is the easiest way to place them on a sheet
someplace, run sort, and then read them back in?

Don <donwiss at panix.com.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 339
Default Sorting in a macro


"Don Wiss" wrote in message
...
I have xl2002. In in VBA. I have a vector (or one column matrix) of short
strings. I'd like to sort them. Is the easiest way to place them on a

sheet
someplace, run sort, and then read them back in?

Don <donwiss at panix.com.


Use Data|Sort...


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 300
Default Sorting in a macro

Fredrik Wahlgren wrote:

Don Wiss wrote:
I have xl2002. In in VBA. I have a vector (or one column matrix) of short
strings. I'd like to sort them. Is the easiest way to place them on a sheet
someplace, run sort, and then read them back in?


Use Data|Sort...


Uh, what does this have to do with a macro? I know how to use a macro to
sort on a sheet. I asked if that was the only way, as the strings are in a
macro and will end up only in the macro.

Don <donwiss at panix.com.
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Sorting in a macro

This example builds an array of filenames and then sorts them.

Sub UpdateFileLB()
Dim Fname As String, FileArray() As String
Dim FCounter As Long
Fname = Dir("C:\excel\files\*.xls")
Do While Fname < ""
FCounter = FCounter + 1
ReDim Preserve FileArray(1 To FCounter)
FileArray(FCounter) = Fname
Fname = Dir()
Loop
QuickSort FileArray, 1, FCounter
End Sub

Sub QuickSort(SortArray, L As Long, R As Long)
' L is the lower bound of the array
' R is the upper bound of the array
Dim I As Long, J As Long, x As Variant, Y As Variant
I = L
J = R
x = SortArray((L + R) / 2)
While (I <= J)
While (SortArray(I) < x And I < R)
I = I + 1
Wend
While (x < SortArray(J) And J L)
J = J - 1
Wend
If (I <= J) Then
Y = SortArray(I)
SortArray(I) = SortArray(J)
SortArray(J) = Y
I = I + 1
J = J - 1
End If
Wend
If (L < J) Then Call QuickSort(SortArray, L, J)
If (I < R) Then Call QuickSort(SortArray, I, R)
End Sub

--
Regards,
Tom Ogilvy


"Don Wiss" wrote in message
...
I have xl2002. In in VBA. I have a vector (or one column matrix) of short
strings. I'd like to sort them. Is the easiest way to place them on a

sheet
someplace, run sort, and then read them back in?

Don <donwiss at panix.com.



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 300
Default Sorting in a macro

On Sun, 9 Jan 2005 19:41:53 -0500, "Tom Ogilvy" wrote:

This example builds an array of filenames and then sorts them.


Sub QuickSort(SortArray, L As Long, R As Long)


[snipped..]

Thanks. Works beautifully. Now after this I will remove duplicates.
Possibly something that could (optionally) be inside QuickSort.

Don <donwiss at panix.com.


  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,758
Default Sorting in a macro

You may want to look at the way John Walkenbach did it:
http://j-walk.com/ss/excel/tips/tip47.htm



Don Wiss wrote:

On Sun, 9 Jan 2005 19:41:53 -0500, "Tom Ogilvy" wrote:

This example builds an array of filenames and then sorts them.


Sub QuickSort(SortArray, L As Long, R As Long)


[snipped..]

Thanks. Works beautifully. Now after this I will remove duplicates.
Possibly something that could (optionally) be inside QuickSort.

Don <donwiss at panix.com.


--

Dave Peterson
  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 300
Default Sorting in a macro

On Sun, 09 Jan 2005, Dave Peterson wrote:

Don Wiss wrote:
Now after this I will remove duplicates.
Possibly something that could (optionally) be inside QuickSort.


You may want to look at the way John Walkenbach did it:
http://j-walk.com/ss/excel/tips/tip47.htm


Interesting. I have never used a collection. Nor been aware of them. Though
now in sorted order removing duplicates is trivial, and I do need it to be
sorted for the next step.

Don <donwiss at panix.com.
  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,758
Default Sorting in a macro

John had a sort in that sample code, too.

Don Wiss wrote:

On Sun, 09 Jan 2005, Dave Peterson wrote:

Don Wiss wrote:
Now after this I will remove duplicates.
Possibly something that could (optionally) be inside QuickSort.


You may want to look at the way John Walkenbach did it:
http://j-walk.com/ss/excel/tips/tip47.htm


Interesting. I have never used a collection. Nor been aware of them. Though
now in sorted order removing duplicates is trivial, and I do need it to be
sorted for the next step.

Don <donwiss at panix.com.


--

Dave Peterson
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
Help with Macro for sorting Brad[_6_] Excel Worksheet Functions 2 August 10th 09 04:20 PM
sorting using macro Ross Excel Discussion (Misc queries) 2 September 15th 05 10:13 PM
Macro for Sorting SiRCYRO Excel Programming 0 September 9th 03 07:35 PM
Sorting with a macro? gschimek Excel Programming 2 August 29th 03 09:57 PM
Sorting w/Macro Joe[_24_] Excel Programming 1 August 28th 03 07:44 PM


All times are GMT +1. The time now is 10:22 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"