Sorting Arrays

Sorting Arrays

Anonymous
Not applicable
342 Views
6 Replies
Message 1 of 7

Sorting Arrays

Anonymous
Not applicable
Hi to All,
I need to sort the contents of a (2) dimensional (table) array before
sending the data to an Excel spreadsheet. The array has (4) columns and may
hold several thousand rows. I need to sort all the rows by the first column
and then by the second column. Any advise on how to tackle this problem? I
was also wondering if I can programatically sort the data in the spreadsheet
through VBA. If so, which way is better, sort the data before sending it to
Excel or sort the data in Excel programatically.
0 Likes
343 Views
6 Replies
Replies (6)
Message 2 of 7

Anonymous
Not applicable
You could do a test to see which way is better, but I think it's going to
depend on how your sorting code is structured and how long the arrays are.
Here's how to do it in the spreadsheet; you might do a search for 'sorting
algorithm', or something like that on google to see what the code might look
like.

Dim sRange As String
Dim i As Long

i = 1000 'this is your ubound of array, or maximum row number in
spreadsheet
sRange = CStr(1) & ":" & CStr(i + 1)
Rows(sRange).Sort _
Key1:=Cells(1, 1), _
Order1:=xlAscending, _
Header:=xlGuess, _
OrderCustom:=1, _
MatchCase:=False, _
Orientation:=xlTopToBottom


--
Kevin


"Gordon Reichhardt" wrote in message
news:863948D2509F1D7D91FB31F891D80FEA@in.WebX.maYIadrTaRb...
> Hi to All,
> I need to sort the contents of a (2) dimensional (table) array before
> sending the data to an Excel spreadsheet. The array has (4) columns and
may
> hold several thousand rows. I need to sort all the rows by the first
column
> and then by the second column. Any advise on how to tackle this problem?
I
> was also wondering if I can programatically sort the data in the
spreadsheet
> through VBA. If so, which way is better, sort the data before sending it
to
> Excel or sort the data in Excel programatically.
>
>
0 Likes
Message 3 of 7

Anonymous
Not applicable
FWIW,
I've heard of sorting arrays by adding them to a listbox, letting VB sort
them, and then retrieving them. Listboxes have a property to auto-sort
added items. I don't know if this could be made to work with 4 columns or
not, or if it will sort your data types properly, but here's some links.

http://groups.google.com/groups?q=vb+listbox+sort&ie=UTF-8&oe=UTF-8&hl=en

James

"Gordon Reichhardt" wrote in message
news:863948D2509F1D7D91FB31F891D80FEA@in.WebX.maYIadrTaRb...
> Hi to All,
> I need to sort the contents of a (2) dimensional (table) array before
> sending the data to an Excel spreadsheet. The array has (4) columns and
may
> hold several thousand rows. I need to sort all the rows by the first
column
> and then by the second column. Any advise on how to tackle this problem?
I
> was also wondering if I can programatically sort the data in the
spreadsheet
> through VBA. If so, which way is better, sort the data before sending it
to
> Excel or sort the data in Excel programatically.
>
>
0 Likes
Message 4 of 7

Anonymous
Not applicable
Thanx Kevin,
I've implemented your code in my project and it works but the code only
sorts the first column. I need to have the data sorted by the first column
and then by the second column. How do I do this?

"Kevin Terry" wrote in message
news:F8B1A7342B7418158A5F667DBBED26D5@in.WebX.maYIadrTaRb...
> You could do a test to see which way is better, but I think it's going to
> depend on how your sorting code is structured and how long the arrays are.
> Here's how to do it in the spreadsheet; you might do a search for 'sorting
> algorithm', or something like that on google to see what the code might
look
> like.
>
> Dim sRange As String
> Dim i As Long
>
> i = 1000 'this is your ubound of array, or maximum row number in
> spreadsheet
> sRange = CStr(1) & ":" & CStr(i + 1)
> Rows(sRange).Sort _
> Key1:=Cells(1, 1), _
> Order1:=xlAscending, _
> Header:=xlGuess, _
> OrderCustom:=1, _
> MatchCase:=False, _
> Orientation:=xlTopToBottom
>
>
> --
> Kevin
>
>
> "Gordon Reichhardt" wrote in message
> news:863948D2509F1D7D91FB31F891D80FEA@in.WebX.maYIadrTaRb...
> > Hi to All,
> > I need to sort the contents of a (2) dimensional (table) array
before
> > sending the data to an Excel spreadsheet. The array has (4) columns and
> may
> > hold several thousand rows. I need to sort all the rows by the first
> column
> > and then by the second column. Any advise on how to tackle this
problem?
> I
> > was also wondering if I can programatically sort the data in the
> spreadsheet
> > through VBA. If so, which way is better, sort the data before sending
it
> to
> > Excel or sort the data in Excel programatically.
> >
> >
>
>
0 Likes
Message 5 of 7

Anonymous
Not applicable
Never mind, I figured it out. Thanx again for your help.

"Gordon Reichhardt" wrote in message
news:AD725FD2B7433AD14D55E89D6AF487B8@in.WebX.maYIadrTaRb...
> Thanx Kevin,
> I've implemented your code in my project and it works but the code only
> sorts the first column. I need to have the data sorted by the first
column
> and then by the second column. How do I do this?
>
> "Kevin Terry" wrote in message
> news:F8B1A7342B7418158A5F667DBBED26D5@in.WebX.maYIadrTaRb...
> > You could do a test to see which way is better, but I think it's going
to
> > depend on how your sorting code is structured and how long the arrays
are.
> > Here's how to do it in the spreadsheet; you might do a search for
'sorting
> > algorithm', or something like that on google to see what the code might
> look
> > like.
> >
> > Dim sRange As String
> > Dim i As Long
> >
> > i = 1000 'this is your ubound of array, or maximum row number in
> > spreadsheet
> > sRange = CStr(1) & ":" & CStr(i + 1)
> > Rows(sRange).Sort _
> > Key1:=Cells(1, 1), _
> > Order1:=xlAscending, _
> > Header:=xlGuess, _
> > OrderCustom:=1, _
> > MatchCase:=False, _
> > Orientation:=xlTopToBottom
> >
> >
> > --
> > Kevin
> >
> >
> > "Gordon Reichhardt" wrote in message
> > news:863948D2509F1D7D91FB31F891D80FEA@in.WebX.maYIadrTaRb...
> > > Hi to All,
> > > I need to sort the contents of a (2) dimensional (table) array
> before
> > > sending the data to an Excel spreadsheet. The array has (4) columns
and
> > may
> > > hold several thousand rows. I need to sort all the rows by the first
> > column
> > > and then by the second column. Any advise on how to tackle this
> problem?
> > I
> > > was also wondering if I can programatically sort the data in the
> > spreadsheet
> > > through VBA. If so, which way is better, sort the data before sending
> it
> > to
> > > Excel or sort the data in Excel programatically.
> > >
> > >
> >
> >
>
>
0 Likes
Message 6 of 7

Anonymous
Not applicable
James,
I think that's only for VB listboxes - not VBA ones.

--
Kevin


"James Belshan" wrote in message
news:D00FD642FD3AC622974B01F64FF7256D@in.WebX.maYIadrTaRb...
> FWIW,
> I've heard of sorting arrays by adding them to a listbox, letting VB sort
> them, and then retrieving them. Listboxes have a property to auto-sort
> added items. I don't know if this could be made to work with 4 columns or
> not, or if it will sort your data types properly, but here's some links.
>
> http://groups.google.com/groups?q=vb+listbox+sort&ie=UTF-8&oe=UTF-8&hl=en
>
> James
>
> "Gordon Reichhardt" wrote in message
> news:863948D2509F1D7D91FB31F891D80FEA@in.WebX.maYIadrTaRb...
> > Hi to All,
> > I need to sort the contents of a (2) dimensional (table) array
before
> > sending the data to an Excel spreadsheet. The array has (4) columns and
> may
> > hold several thousand rows. I need to sort all the rows by the first
> column
> > and then by the second column. Any advise on how to tackle this
problem?
> I
> > was also wondering if I can programatically sort the data in the
> spreadsheet
> > through VBA. If so, which way is better, sort the data before sending
it
> to
> > Excel or sort the data in Excel programatically.
> >
> >
>
>
0 Likes
Message 7 of 7

Anonymous
Not applicable
I use the listview control to tremendous success. It has sorting properties and alot of other nice features (listbox just looks to 'DCL' -- former 'lisper'). HTH Lanny
0 Likes