Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default Trouble to sort an array in Excel using VBA

I have a column in a worksheet which let's say looks that way:

1
3
2
4

I would like to sort it in order to get:

1
2
3
4

I tried to adapt this funtion but couldn't get it work... Can anybody help me?
I must miss something here... I always get #VALUE even using SHIFT+CTRL+ENTER

Function BubbleSort(List As Variant)
' Sorts an array using bubble sort algorithm
Dim First As Integer, Last As Integer
Dim i As Integer, j As Integer
Dim Temp As Integer

First = LBound(List)
Last = UBound(List)
For Each Element In List
For j = i + 1 To Last
If List(i) List(j) Then
Temp = List(j)
List(j) = List(i)
List(i) = Temp
End If
Next j
'Next i
Next Element
End Function
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,824
Default Trouble to sort an array in Excel using VBA

If you really meant you wanted the range sorted, record a macro when you sort
that range.

If you wanted to retrieve the values from the range (into an array), then sort
that array without touching the original range:

Option Explicit
Sub test1()

Dim myArray As Variant
Dim iCtr As Long

With ActiveSheet
myArray = Application.Transpose(.Range("a1", _
.Cells(.Rows.Count, "A").End(xlUp)))
End With

BubbleSort myArray

For iCtr = LBound(myArray) To UBound(myArray)
Debug.Print myArray(iCtr)
Next iCtr

End Sub
Function BubbleSort(List As Variant)
' Sorts an array using bubble sort algorithm
Dim First As Integer, Last As Integer
Dim i As Integer, j As Integer
Dim Temp As Integer

First = LBound(List)
Last = UBound(List)
For i = 1 To Last - 1
For j = i + 1 To Last
If List(i) List(j) Then
Temp = List(j)
List(j) = List(i)
List(i) = Temp
End If
Next j
Next i

End Function

choco140 wrote:

I have a column in a worksheet which let's say looks that way:

1
3
2
4

I would like to sort it in order to get:

1
2
3
4

I tried to adapt this funtion but couldn't get it work... Can anybody help me?
I must miss something here... I always get #VALUE even using SHIFT+CTRL+ENTER

Function BubbleSort(List As Variant)
' Sorts an array using bubble sort algorithm
Dim First As Integer, Last As Integer
Dim i As Integer, j As Integer
Dim Temp As Integer

First = LBound(List)
Last = UBound(List)
For Each Element In List
For j = i + 1 To Last
If List(i) List(j) Then
Temp = List(j)
List(j) = List(i)
List(i) = Temp
End If
Next j
'Next i
Next Element
End Function


--

Dave Peterson

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
Sort Trouble Doug Mc New Users to Excel 5 December 16th 09 05:21 PM
Trouble with Array multiple return formula Kate Excel Discussion (Misc queries) 4 September 29th 09 04:38 PM
Having trouble getting MATCH to work with a variable lookup array Chuck M Excel Worksheet Functions 3 August 20th 08 11:45 PM
sort & expand trouble flo Excel Discussion (Misc queries) 0 February 13th 07 05:21 PM
Trouble shooting#NA error in Array formula RonR Excel Discussion (Misc queries) 2 June 14th 05 09:58 PM


All times are GMT +1. The time now is 09:49 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"