Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 471
Default Sort/Search Arrays

How do you sort an array and then do a binary search on an array in VBA?
Also, can you assign pointers to the array? How?
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10,593
Default Sort/Search Arrays

You can easily sort an array, and it would depend upon whether the array
will be large or not, how many dimensions it has, as to which type of sort
you might use.

Here is a simple bubble sort

Option Explicit

Public Function BubbleSort(InVal As Variant, Optional Order As String =
"Asc")
Dim fChanges As Boolean
Dim iElement As Long
Dim iElement2 As Long
Dim temp As Variant
Dim ToSort

If TypeName(InVal) = "Variant()" Then
ToSort = InVal
Do
fChanges = False
For iElement = LBound(ToSort) To UBound(ToSort) - 1
If ((Order = "Asc" And ToSort(iElement) ToSort(iElement +
1)) Or _
(Order < "Asc" And ToSort(iElement) < ToSort(iElement +
1))) Then
'Swap elements
temp = ToSort(iElement)
ToSort(iElement) = ToSort(iElement + 1)
ToSort(iElement + 1) = temp
fChanges = True
End If
Next iElement
Loop Until Not fChanges
ElseIf TypeName(InVal) = "Range" Then
ToSort = InVal
ReDim temp(LBound(ToSort, 2) To UBound(ToSort, 2))
Do
fChanges = False
For iElement = LBound(ToSort) To UBound(ToSort) - 1
If ((Order = "Asc" And ToSort(iElement, 1) ToSort(iElement
+ 1, 1)) Or _
(Order < "Asc" And ToSort(iElement, 1) <
ToSort(iElement + 1, 1))) Then
'Swap elements
For iElement2 = LBound(ToSort, 2) To UBound(ToSort, 2)
temp(iElement2) = ToSort(iElement, iElement2)
ToSort(iElement, iElement2) = ToSort(iElement + 1,
iElement2)
ToSort(iElement + 1, iElement2) = temp(iElement2)
Next iElement2
fChanges = True
End If
Next iElement
Loop Until Not fChanges
End If
BubbleSort = ToSort
End Function



Sub testbubblesort()
Dim ary
ary = Array(1, "Bill", 4, 3, 6, "Alan", "Barry", 2)
ary = BubbleSort(ary)
ary = BubbleSort(Range("B1:D10"))
End Sub


--
---
HTH

Bob

(there's no email, no snail mail, but somewhere should be gmail in my addy)



"Mike H." wrote in message
...
How do you sort an array and then do a binary search on an array in VBA?
Also, can you assign pointers to the array? How?



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 471
Default Sort/Search Arrays

I am having trouble getting this code to work. When I call the function
1. Is the INVal the array I am passing?
2. The "Dim To Sort" line doesn't make sense to me. Don't you have to Dim
it as something?
Thanks...


Option Explicit

Public Function BubbleSort(InVal As Variant, Optional Order As String =
"Asc")
Dim fChanges As Boolean
Dim iElement As Long
Dim iElement2 As Long
Dim temp As Variant
Dim ToSort

If TypeName(InVal) = "Variant()" Then
ToSort = InVal
Do
fChanges = False
For iElement = LBound(ToSort) To UBound(ToSort) - 1
If ((Order = "Asc" And ToSort(iElement) ToSort(iElement +
1)) Or _
(Order < "Asc" And ToSort(iElement) < ToSort(iElement +
1))) Then
'Swap elements
temp = ToSort(iElement)
ToSort(iElement) = ToSort(iElement + 1)
ToSort(iElement + 1) = temp
fChanges = True
End If
Next iElement
Loop Until Not fChanges
ElseIf TypeName(InVal) = "Range" Then
ToSort = InVal
ReDim temp(LBound(ToSort, 2) To UBound(ToSort, 2))
Do
fChanges = False
For iElement = LBound(ToSort) To UBound(ToSort) - 1
If ((Order = "Asc" And ToSort(iElement, 1) ToSort(iElement
+ 1, 1)) Or _
(Order < "Asc" And ToSort(iElement, 1) <
ToSort(iElement + 1, 1))) Then
'Swap elements
For iElement2 = LBound(ToSort, 2) To UBound(ToSort, 2)
temp(iElement2) = ToSort(iElement, iElement2)
ToSort(iElement, iElement2) = ToSort(iElement + 1,
iElement2)
ToSort(iElement + 1, iElement2) = temp(iElement2)
Next iElement2
fChanges = True
End If
Next iElement
Loop Until Not fChanges
End If
BubbleSort = ToSort
End Function



Sub testbubblesort()
Dim ary
ary = Array(1, "Bill", 4, 3, 6, "Alan", "Barry", 2)
ary = BubbleSort(ary)
ary = BubbleSort(Range("B1:D10"))
End Sub


--
---
HTH

Bob

(there's no email, no snail mail, but somewhere should be gmail in my addy)



"Mike H." wrote in message
...
How do you sort an array and then do a binary search on an array in VBA?
Also, can you assign pointers to the array? How?




Reply
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
Search/Sort Formula Help Needed JB Excel Discussion (Misc queries) 0 February 10th 10 06:16 PM
SEARCH and arrays Janie Excel Worksheet Functions 1 June 11th 08 01:50 AM
How Do I Sort search results? Charlie Excel Programming 1 July 31st 07 02:44 PM
How can I sort or search the sheet tabs? spippin Excel Worksheet Functions 3 October 24th 06 05:19 PM
Fastest way to sort large 2-D arrays? RB Smissaert Excel Programming 21 January 10th 05 07:12 PM


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