Array question

G

Guest

I am having a problem with arrays I am trying to add to an array with 3
columns, I had to use the code below but how can I sort it by the first
column or is there another way of populating the array
Regards
Charles

ElimentCount = ElimentCount + 1
ReDim Preserve myArray(1 To 3, 1 To ElimentCount)

myArray(1, ElimentCount) = vqSize
myArray(2, ElimentCount) = vqRoot
myArray(3, ElimentCount) = fName
 
G

Guest

if you want to use a dynamic array and redim preserve, then you will have to
use the approach you show.

Here is a bubble sort to sort on the first row ("column" in your transposed
array)
Function SortArray(myArray)
Dim temp, i, j, i
For i = LBound(myArray, 2) To UBound(myArray, 2) - 1
For j = i + 1 To UBound(myArray, 2)
If myArray(1, i) > myArray(1, j) Then
For k = LBound(myArray, 1) To UBound(myArray, 1)
temp = myArray(k, i)
myArray(k, i) = myArray(k, j)
myArray(k, j) = temp
Next
End If
Next
Next
SortArray = myArray
End Function

Here is a test function used to test it.
Put random data in A1:F2, then run the Tester macro

Sub Tester()
Dim myArray
myArray = Range("A1:F3").Value
myArray = SortArray(myArray)
Range("A1:F3") = myArray
End Sub
 
B

Bob Phillips

Just change the > to < in the 5th line of the function.

--
HTH

Bob Phillips

(replace somewhere in email address with gmail if mailing direct)
 
G

Guest

Change the >to< to what?
Regards
Charles

Bob Phillips said:
Just change the > to < in the 5th line of the function.

--
HTH

Bob Phillips

(replace somewhere in email address with gmail if mailing direct)
 
B

Bob Phillips

Here is a bubble sort to sort on the first row ("column" in your transposed
array)
Function SortArray(myArray)
Dim temp, i, j, i
For i = LBound(myArray, 2) To UBound(myArray, 2) - 1
For j = i + 1 To UBound(myArray, 2)
If myArray(1, i) > myArray(1, j) Then '<<<<<<<<<<<<<<<<<<<<< HERE
For k = LBound(myArray, 1) To UBound(myArray, 1)
temp = myArray(k, i)
myArray(k, i) = myArray(k, j)
myArray(k, j) = temp
Next
End If
Next
Next
SortArray = myArray
End Function

--
HTH

Bob Phillips

(replace somewhere in email address with gmail if mailing direct)
 
G

Guest

Thanks for the help.
Regards
Charles

Bob Phillips said:
Here is a bubble sort to sort on the first row ("column" in your transposed
array)
Function SortArray(myArray)
Dim temp, i, j, i
For i = LBound(myArray, 2) To UBound(myArray, 2) - 1
For j = i + 1 To UBound(myArray, 2)
If myArray(1, i) > myArray(1, j) Then '<<<<<<<<<<<<<<<<<<<<< HERE
For k = LBound(myArray, 1) To UBound(myArray, 1)
temp = myArray(k, i)
myArray(k, i) = myArray(k, j)
myArray(k, j) = temp
Next
End If
Next
Next
SortArray = myArray
End Function

--
HTH

Bob Phillips

(replace somewhere in email address with gmail if mailing direct)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top