LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,290
Default Sorting 3 columns in a 2 dimension array

John,
My modified version of the code below is much faster,
however, it is still very slow on large arrays.
--
Jim Cone
San Francisco, USA
http://www.realezsites.com/bus/primitivesoftware


Function SortArrayLikeXlSheet(ByRef varArray As Variant, _
ByRef Col_1 As Long, ByRef Col_2 As Long, _
ByRef Col_3 As Long) As Variant
Dim blnCondition As Boolean
Dim i As Long
Dim j As Long
Dim Y As Long
Dim vTemp As Variant
Dim lngTop As Long
Dim lngBottom As Long
Dim lngLeft As Long
Dim lngRight As Long

lngTop = LBound(varArray, 1)
lngBottom = UBound(varArray, 1)
lngLeft = LBound(varArray, 2)
lngRight = UBound(varArray, 2)

For i = lngTop To lngBottom - 1
For j = i + 1 To lngBottom
Select Case True
Case varArray(i, Col_1) varArray(j, Col_1)
blnCondition = True
Case (varArray(i, Col_1) = varArray(j, Col_1)) And _
(varArray(i, Col_2) varArray(j, Col_2))
blnCondition = True
Case (varArray(i, Col_1) = varArray(j, Col_1)) And _
(varArray(i, Col_2) = varArray(j, Col_2)) And _
(varArray(i, Col_3) varArray(j, Col_3))
blnCondition = True
Case Else
blnCondition = False
End Select
If blnCondition Then
For Y = lngLeft To lngRight
vTemp = varArray(i, Y)
varArray(i, Y) = varArray(j, Y)
varArray(j, Y) = vTemp
Next 'y
End If
Next 'j
Next 'i
SortArrayLikeXlSheet = varArray
End Function

Sub PutArrayInOrder()
Dim vArry As Variant
vArry = Selection.Value
'Specify order in which columns to be sorted.
Call SortArrayLikeXlSheet(vArry, 2, 3, 1)
Selection.Value = vArry
End Sub
'-----------


"John"
wrote in message
With a little help from the Internet world, I have a working solution.

For those interested, go to
http://www.excelwiki.com/VBA/Arrays-BubbleSort for the following
snippet:

SortColumn1=0
SortColumn2=3 '<--changed to 1
SortColumn3=5 '<--changed to 2
For i = LBound(ArrayName, 1) To UBound(ArrayName, 1) - 1
For j = LBound(ArrayName, 1) To UBound(ArrayName, 1) - 1
Condition1=ArrayName(j, SortColumn1) ArrayName(j + 1,
SortColumn1)
Condition2=ArrayName(j, SortColumn1) = ArrayName(j + 1,
SortColumn1) and _
ArrayName(j, SortColumn2) ArrayName(j + 1,
SortColumn2)
Condition3=ArrayName(j, SortColumn1) = ArrayName(j + 1,
SortColumn1) and _
ArrayName(j, SortColumn2) = ArrayName(j + 1,
SortColumn2) and _
ArrayName(j, SortColumn3) ArrayName(j + 1,
SortColumn3)

If Condition1 or Condition2 or Condition3 Then
For y = LBound(ArrayName, 2) To UBound(ArrayName, 2)
t = ArrayName(j, y)
ArrayName(j, y) = ArrayName(j + 1, y)
ArrayName(j + 1, y) = t
Next y
End If
Next
Next

This seems to be both scalable and flexible.
Thx ... John

John wrote:
Hello ...

I would like to sort a 3-column combo box list; ordered by Column1
then by Column2 then by Column3.

Have looked at Tom Oglviy's dialogue with Tim Zych (Nov 5, 2002), but
am not sure how to scale the VBA code to accomplish my task.

Would appreciate any help.

John


 
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
Mutli-dimensional Array to Single-Dimension Array Blue Aardvark Excel Programming 3 October 15th 05 09:22 AM
HOW TO ASSIGN 2 DIMENSION ARRAY VALUES FROM 2 COLUMNS? -JEFF-[_2_] Excel Programming 3 August 12th 05 05:36 PM
Array transfer - 1 dimension v. 2 dimension JWolf[_2_] Excel Programming 2 June 29th 04 01:02 AM
Getting excel array dimension banavas[_3_] Excel Programming 3 June 8th 04 12:11 PM
single dimension array RobcPettit Excel Programming 3 January 20th 04 08:33 AM


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