Você está na página 1de 9

Answered by:

Top related threads


Excel VBA - Sort an array
Sorting a 2 dimension array in Excel
VBA
Converting Excel vba addin to VSTO -
Problem using vba function array
Array sort with custom function
VBA Excel , Arrays multiplication using
SumProduct WorkSheet function
Ask a question
Quick access
Search related threads En Search forum questions
11 6 3
Hans Vogelaar MVP's threads
View Profile
64,455 Points
I've been using Excel since 1986 (version
1.0 for the Macintosh), Word and Access
since the mid '90s. I have been an MVP for
Access since October 1, 2010.
Hans Vogelaar MVP
MCC, MVP
www.eileenslounge.com
Microsoft ISV Community Center >
Sorting An Array in VBA (without excel function)
Visual Basic for Applications (VBA)
Developer Network
MSDN subscriptions Get tools Sign in
Home Technologies Dev centers Connect Downloads Library Samples
Follow us
Tech Advisors Events Forums
Pgina 1 de 9 Sorting An Array in VBA (without excel function)
28/04/2014 http://social.msdn.microsoft.com/Forums/en-US/830b42cf-8c97-4aaf-b34b-d860773281f...
Question
0
Sign in
to vote
Reply | Quote |
Saturday, March 24, 2012 6:56 PM
25 Points
Hi Experts,
1. I want to sort an Array in VBA. For strings, it should sort per alphabets, and ascending
for numbers.
2. I have accomplished this using Excel Sort Method. (passing array values into cells).
3. But for learning sake, now I want to do it without Excel functions.
4. My search led me to three methods :
System namespace (using Array Class),
Bubble Sort
Own custom function
5. As you can see in images, I am not able to use System namespace method, or either I don't
know how to use it properly.
6. I have not a slightest idea about Bubble Sort.
7. So far, I have been able to create custom functions to get max and min value from an array.
But, sorting an array, getting 3rd highest or lowest number, or getting unique valuesis uphill
task for me.
8. Can you guide me how to use System.Array. method effectively or build custom function for it.
9. Even a reference to some easy tutorial, or article would suffice, provided that it has
all informationexplained in nice manner.
With regard : Ajay Check
Edited by Ajay Check Saturday, March 24, 2012 7:03 PM Adding info
Answers
Ajay Check
Pgina 2 de 9 Sorting An Array in VBA (without excel function)
28/04/2014 http://social.msdn.microsoft.com/Forums/en-US/830b42cf-8c97-4aaf-b34b-d860773281f...
1
Sign in
to vote
Reply | Quote |
Saturday, March 24, 2012 11:02 PM
(MCC, MVP) 64,455 Points
System.Array is VB.Net, I tthink, not VBA.
See http://en.wikipedia.org/wiki/Sorting_algorithmfor general information about sorting
algorithms.
You'll find much more info if you search Google or Bing.
Regards, Hans Vogelaar
Marked as answer by Ajay Check Saturday, March 24, 2012 11:42 PM
All replies
2
Sign in
to vote
Reply | Quote |
Saturday, March 24, 2012 7:08 PM
(MCC, MVP) 64,455 Points
Here is a very simple bubble sort procedure:
Sub BubbleSort(arr)
Dim strTemp As String
Dim i As Long
Dim j As Long
Dim lngMin As Long
Dim lngMax As Long
lngMin = LBound(arr)
lngMax = UBound(arr)
For i = lngMin To lngMax - 1
For j = i + 1 To lngMax
If arr(i) > arr(j) Then
strTemp = arr(i)
arr(i) = arr(j)
arr(j) = strTemp
End If
Next j
Next i
End Sub
To sort your array MyArray, use
BubbleSort MyArray
or
Call BubbleSort(MyArray)
BubbleSort works fine for arrays with up to a few hundreds of elements, but execution time
quickly gets out of hand for larger arrays (execution time is proportional to the square of the
number of elements in the array). For large arrays, use QuickSort, for example.
Regards, Hans Vogelaar
Proposed as answer by Keith Ruck Wednesday, April 24, 2013 7:46 PM
Unproposed as answer by Keith Ruck Wednesday, April 24, 2013 7:47 PM
Hans Vogelaar MVP
Hans Vogelaar MVP
Pgina 3 de 9 Sorting An Array in VBA (without excel function)
28/04/2014 http://social.msdn.microsoft.com/Forums/en-US/830b42cf-8c97-4aaf-b34b-d860773281f...
2
Sign in
to vote
Reply | Quote |
Saturday, March 24, 2012 7:20 PM
(MCC, MVP) 64,455 Points
And here is a QuickSort implementation:
Sub QuickSort(arr, Lo As Long, Hi As Long)
Dim varPivot As Variant
Dim varTmp As Variant
Dim tmpLow As Long
Dim tmpHi As Long
tmpLow = Lo
tmpHi = Hi
varPivot = arr((Lo + Hi) \ 2)
Do While tmpLow <= tmpHi
Do While arr(tmpLow) < varPivot And tmpLow < Hi
tmpLow = tmpLow + 1
Loop
Do While varPivot < arr(tmpHi) And tmpHi > Lo
tmpHi = tmpHi - 1
Loop
If tmpLow <= tmpHi Then
varTmp = arr(tmpLow)
arr(tmpLow) = arr(tmpHi)
arr(tmpHi) = varTmp
tmpLow = tmpLow + 1
tmpHi = tmpHi - 1
End If
Loop
If Lo < tmpHi Then QuickSort arr, Lo, tmpHi
If tmpLow < Hi Then QuickSort arr, tmpLow, Hi
End Sub
Use like this:
QuickSort MyArray, LBound(MyArray), UBound(MyArray)
or
Call QuickSort(MyArray, LBound(MyArray), UBound(MyArray))
For small arrays, QuickSort is overkill, but for large arrays it is much more efficient than
BubbleSort.
Regards, Hans Vogelaar
Hans Vogelaar MVP
Pgina 4 de 9 Sorting An Array in VBA (without excel function)
28/04/2014 http://social.msdn.microsoft.com/Forums/en-US/830b42cf-8c97-4aaf-b34b-d860773281f...
0
Sign in
to vote
Reply | Quote |
Saturday, March 24, 2012 7:30 PM
25 Points
Hi,@ Hans VogelaarMVP
1. I integrated.your code and it was called successfully.
2. Array was supposed to get sorted, but in immediate window I couldn't see desired result.
Immediate window doesn't show any sorting pattern.
3. Did I do anything wrong in using code or displaying results.
1
Sign in
to vote
Reply | Quote |
Saturday, March 24, 2012 8:05 PM
(MCC, MVP) 64,455 Points
If you read my reply carefully, you'll see that you have to use either
BubbleSort MyArray
or
Call BubbleSort(MyArray)
and NOT !!!
BubbleSort (MyArray)
That syntax passes MyArray as a by value argument instead of as a by reference argument, as
intended.
Regards, Hans Vogelaar
Ajay Check
Hans Vogelaar MVP
Pgina 5 de 9 Sorting An Array in VBA (without excel function)
28/04/2014 http://social.msdn.microsoft.com/Forums/en-US/830b42cf-8c97-4aaf-b34b-d860773281f...
0
Sign in
to vote
Reply | Quote |
Saturday, March 24, 2012 10:25 PM
25 Points
That syntax passes MyArray as a by value argument instead of as a by reference argument, as
intended.
Regards, Hans Vogelaar
Hi, Mr Vogelaar
Thanks again. . .It worked ;-)
Oops, . .I was in so hurry . .to test this code that I overlooked the fact : With array's all that
matters is reference.
This has been good example for me, about passing arguments in, by ref or by val way.
Meanwhile I just went through Bubble Sort Code, to find about its working, which I have got now.
Would be doing same for Quick Sort Code
Need bit of light on . . .
How do I access System.Array namespace in Excel VBA to experiment with array's.
How do I sort multidimensional array's.
How do I sort strings (data type) in various forms in one/dimensional dimensional array.
If explanation for above is too long or code for same is too much complex and lengthy than
Quick Sort one, then a good reference to any article/tutorial/guide would be helpful, because I
want to know basic on which code is written before blindly using it.
With Regards : Ajay Check
Edited by Ajay Check Saturday, March 24, 2012 10:33 PM
Ajay Check
Pgina 6 de 9 Sorting An Array in VBA (without excel function)
28/04/2014 http://social.msdn.microsoft.com/Forums/en-US/830b42cf-8c97-4aaf-b34b-d860773281f...
1
Sign in
to vote
Reply | Quote |
Saturday, March 24, 2012 11:02 PM
(MCC, MVP) 64,455 Points
System.Array is VB.Net, I tthink, not VBA.
See http://en.wikipedia.org/wiki/Sorting_algorithmfor general information about sorting
algorithms.
You'll find much more info if you search Google or Bing.
Regards, Hans Vogelaar
Marked as answer by Ajay Check Saturday, March 24, 2012 11:42 PM
0
Sign in
to vote
Reply | Quote |
Saturday, March 24, 2012 11:35 PM
25 Points
System.Array is VB.Net, I tthink, not VBA.
See http://en.wikipedia.org/wiki/Sorting_algorithmfor general information about sorting
algorithms.
You'll find much more info if you search Google or Bing.
Regards, Hans Vogelaar
Given link is wonderful. . . .
Forever in debt, for solving queries about array's, in mere 4 hours with actual code and valid
references, which otherwise would have taken parsing of tons of Google pages, and ages to read
and understand them.
With Regards : Ajay Check
Hans Vogelaar MVP
Ajay Check
Pgina 7 de 9 Sorting An Array in VBA (without excel function)
28/04/2014 http://social.msdn.microsoft.com/Forums/en-US/830b42cf-8c97-4aaf-b34b-d860773281f...
0
Sign in
to vote
Reply | Quote |
Tuesday, September 18, 2012 1:05 PM
0 Points
Hi Hans
I have tried the quick sort but it appears to sort by absolute value. the values stored in the array
reflect the actual values (both +ve and -ve). Code is based on code similar to yours above and is
as follows:
Sub QuickSort(ByRef Field As Variant, ByVal LB As Long, ByVal UB As Long)
'FUNCTION:
' sort the array in order of lowest to highest
Dim P1, P2 As Long
Dim Ref As String
Dim strTempIndex As String
Dim dTempData As Double
Const ciIndexCol As Integer = 1
Const ciDataCol As Integer = 2
P1 = LB
P2 = UB
Ref = Field(ciDataCol, (P1 + P2) / 2)
Do
Do While (Field(ciDataCol, P1) < Ref)
P1 = P1 + 1
Loop
Do While (Field(ciDataCol, P2) > Ref)
P2 = P2 - 1
Loop
If P1 <= P2 Then
strTempIndex = Field(ciIndexCol, P1)
dTempData = Field(ciDataCol, P1)
Field(ciIndexCol, P1) = Field(ciIndexCol, P2)
Field(ciDataCol, P1) = Field(ciDataCol, P2)
Field(ciDataCol, P2) = dTempData
Field(ciIndexCol, P2) = strTempIndex
P1 = P1 + 1
P2 = P2 - 1
End If
Loop Until (P1 > P2)
If LB < P2 Then Call QuickSort(Field, LB, P2)
If P1 < UB Then Call QuickSort(Field, P1, UB)
End Sub
0
Sign in
to vote
Reply | Quote |
Tuesday, September 18, 2012 2:01 PM
(MCC, MVP) 64,455 Points
You use the variable Refto compare the values in the data column, but you have declared Ref
asa String, so you get string comparisons. Change Ref to a Double to get the correct result.
Regards, Hans Vogelaar
OneBigDuck
Hans Vogelaar MVP
Pgina 8 de 9 Sorting An Array in VBA (without excel function)
28/04/2014 http://social.msdn.microsoft.com/Forums/en-US/830b42cf-8c97-4aaf-b34b-d860773281f...
0
Sign in
to vote
Reply | Quote |
Wednesday, September 19, 2012 9:34 AM
0 Points
Hans, you legend!!!!
I would never have spotted that one.
thanks
OneBigDuck
Dev centers
Windows
Windows Phone
Office
Visual Studio
Nokia
Microsoft Azure
More...
Learning resources
Microsoft Virtual Academy
Channel 9
Interoperability Bridges
MSDN Magazine
Community
Forums
Blogs
Codeplex
Support
Self support
Other support options
Programs
BizSpark (for startups)
DreamSpark
Faculty Connection
Microsoft Student
United States (English) Newsletter Privacy & cookies Terms of use Trademarks 2014 Microsoft
Pgina 9 de 9 Sorting An Array in VBA (without excel function)
28/04/2014 http://social.msdn.microsoft.com/Forums/en-US/830b42cf-8c97-4aaf-b34b-d860773281f...

Você também pode gostar