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: 493
Default Sorting a 2-dimensional array

Since formulas can't change the values in other cells, what you're
asking isn't possible. Even if they could, you'd have circular
references to deal with.

In addition, you don't really have a two-dimensional array - it's
really three-D since there are two 5x1 areas (which is why you get
the multiple selections error if you try to sort them manually).

Instead of a formula, you could use an event macro to sort when the
data changes. One way:

Put this in the worksheet code module (right-click the worksheet tab
and choose view code):

Private Sub Worksheet_Change(ByVal Target As Excel.Range)
If Target.Column = 4 Then sortDandA
End Sub

and put this in a regular code module (insert/module in the VBE):

Public Sub sortDandA()
Dim arr1 As Variant
Dim arr2 As Variant
Dim temp1 As Variant
Dim temp2 As Variant
Dim rng As Range
Dim i As Integer
Dim j As Integer
Dim chg As Boolean

Set rng = Range("D1:D" & _
Range("D" & Rows.Count).End(xlUp).Row)
arr1 = rng.Value
arr2 = rng.Offset(0, -3).Value
Do
chg = False
For i = 2 To UBound(arr1, 1)
j = i - 1
If arr1(i, 1) arr1(j, 1) Then
temp1 = arr1(i, 1)
temp2 = arr2(i, 1)
arr1(i, 1) = arr1(j, 1)
arr2(i, 1) = arr2(j, 1)
arr1(j, 1) = temp1
arr2(j, 1) = temp2
chg = True
End If
Next i
Loop Until chg = False
Application.EnableEvents = False
rng.Value = arr1
rng.Offset(0, -3).Value = arr2
Application.EnableEvents = True
End Sub

This is fast for relatively small amounts of data. For large
amounts, a faster sort routine should be used.


In article ,
"Randy S" wrote:

I'm looking for a formula that I can enter to do this for
me automatically so I don't have to do Data-Sort
everytime the data changes. I specifically chose Col D to
show the two columns are not adjacent.

 
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
Lookup within a 2 dimensional array Edu Excel Worksheet Functions 5 March 31st 10 09:52 PM
Sorting a Two Dimensional Table FARAZ QURESHI Excel Discussion (Misc queries) 2 April 12th 09 07:10 AM
2-dimensional array to 2 column list TDS Excel Discussion (Misc queries) 3 October 31st 06 05:49 PM
How do I convert a row of cells into a two-dimensional array? Glenn@stress Excel Worksheet Functions 7 October 10th 05 07:28 PM
Three Dimensional Array Question Tornados Excel Discussion (Misc queries) 1 June 20th 05 12:58 AM


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