PC Review


Reply
Thread Tools Rate Thread

How do a sort a multidimensional array(1000,2) in Excel-VBA?

 
 
DJMF
Guest
Posts: n/a
 
      3rd Dec 2007
I have a 2 column array of numerical entries in VBA that I want to sort
lowest to highest by the first column (i.e. I still want to retain the links
between the values in the first and second columns, just like sorting a pair
of columns in a worksheet). I'd prefer not to have to move the numbers into
a worksheet to do this.
 
Reply With Quote
 
 
 
 
Per Jessen
Guest
Posts: n/a
 
      3rd Dec 2007

"DJMF" <(E-Mail Removed)> skrev i en meddelelse
news:825AC524-1426-41CF-9D5B-(E-Mail Removed)...
>I have a 2 column array of numerical entries in VBA that I want to sort
> lowest to highest by the first column (i.e. I still want to retain the
> links
> between the values in the first and second columns, just like sorting a
> pair
> of columns in a worksheet). I'd prefer not to have to move the numbers
> into
> a worksheet to do this.


Hi

Try this code. Fit the array size to your needs.

Option Explicit

Option Base 1
Dim MyArray(10, 2)
Dim MyResultArray(10, 2)
Dim Largest As Double
Dim Smallest As Variant
Dim c As Integer, x As Integer
Dim Counter As Integer
Dim Pointer As Single
Dim CountTo As Double

Sub SortArray()
' Assign data to aray
For c = 1 To 10 ' Assign array
MyArray(c, 1) = Cells(c, 1).Value
MyArray(c, 2) = Cells(c, 2).Value
Next

CountTo = UBound(MyArray, 1)

' Sort Data in array
For c = 1 To CountTo
If MyArray(c, 1) > Largest Then Largest = MyArray(c, 1) ' Largest value
in aray
Next
Smallest = Largest
For c = 1 To CountTo
If MyArray(c, 1) < Smallest Then Smallest = MyArray(c, 1) ' Lowest
value in aray
Next
Pointer = 1
Counter = Smallest
Do Until MyResultArray(CountTo, 1) = Largest
For c = 1 To UBound(MyArray, 1)
If MyArray(c, 1) = Smallest Then
MyResultArray(Pointer, 1) = MyArray(c, 1)
MyResultArray(Pointer, 2) = MyArray(c, 2)
MyArray(c, 1) = True
Pointer = Pointer + 1
Smallest = Largest
For x = 1 To CountTo
If MyArray(x, 1) <> True Then
If MyArray(x, 1) < Smallest Then Smallest = MyArray(x,
1)
End If
Next
End If
Next
Loop
End Sub

Regards,

Per


 
Reply With Quote
 
Ivan
Guest
Posts: n/a
 
      3rd Dec 2007
I am afraid of you have to find a code for sort (for example "bubble" sort),
but you can do also like this: in VBA code put the array in the first two
column of one new empty worksheet, then do sort in the worksheet and after
that take the contents of the worksheet column back into the VBA array.

Ivan

"DJMF" <(E-Mail Removed)> wrote in message
news:825AC524-1426-41CF-9D5B-(E-Mail Removed)...
>I have a 2 column array of numerical entries in VBA that I want to sort
> lowest to highest by the first column (i.e. I still want to retain the
> links
> between the values in the first and second columns, just like sorting a
> pair
> of columns in a worksheet). I'd prefer not to have to move the numbers
> into
> a worksheet to do this.



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
share a structure array containing multidimensional char array C#/ Aykut Ergin Microsoft C# .NET 9 15th Apr 2008 07:40 AM
multidimensional array Peter Microsoft Excel Programming 1 30th Aug 2007 05:34 PM
convert a jagged array into a multidimensional array TS Microsoft C# .NET 5 23rd Jan 2007 12:49 AM
Sort MultiDimensional Array Mushi Microsoft Excel Programming 1 2nd Aug 2006 05:34 AM
Creating an Array from a multidimensional array @ nospam.com Microsoft C# .NET 1 20th Aug 2003 12:13 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 11:46 PM.