Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
KR KR is offline
external usenet poster
 
Posts: 121
Default Request code help, sorting array in memory

I have an array (1 to 100) in memory (I'll never use more than 80-90, but
wanted some extra room just in case). I'm trying to use the following code
to sort the array into alphabetical order before adding the array to a
combobox, but it isn't working. I eliminated all blanks from my array, so
these are just random names in the array (my test set has about 40 names).
Of most importance, I want to sort these names in complete alpha order, even
though the strings are of different length (I'm not sure if that matters, or
how to accomodate if it does, but so far the names don't appear to be
sorting at all)

no matter how I look at it, I just can't figure out what I'm doing wrong.

For i = 99 To 1 Step -1
For j = (i + 1) To 1 Step -1
If FoundNames(i) FoundNames(j) Then
temp = FoundNames(j)
FoundNames(j) = FoundNames(i)
FoundNames(i) = temp
End If
Next
Next

I appreciate your help,
Keith

--
The enclosed questions or comments are entirely mine and don't represent the
thoughts, views, or policy of my employer. Any errors or omissions are my
own.


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Request code help, sorting array in memory

If you won't have any blanks in the lower indexed end of the array, and you
want your spaces at the upper indexed area of the array, you can do:

Sub SortArray()
Dim FoundNames(1 To 100) As String
For i = 1 To 40
For j = 1 To Int(Rnd() * 10 + 1)
FoundNames(i) = FoundNames(i) & Chr(Int(Rnd * 26 + 65))
Next
Next
For i = 1 To 100
Cells(i, 2).Value = FoundNames(i)
Next
For i = 1 To 99
For j = i + 1 To 100
If FoundNames(i) FoundNames(j) And _
FoundNames(j) < "" Then
temp = FoundNames(j)
FoundNames(j) = FoundNames(i)
FoundNames(i) = temp
Debug.Print i, j, FoundNames(i), FoundNames(j)
End If
Next
Next
For i = 1 To 100
Cells(i, 1).Value = FoundNames(i)
Next
End Sub

If you want it sorted without regards to blanks:

Sub SortArray()
Dim FoundNames(1 To 100) As String
For i = 1 To 40
For j = 1 To Int(Rnd() * 10 + 1)
FoundNames(i) = FoundNames(i) & Chr(Int(Rnd * 26 + 65))
Next
Next
For i = 1 To 100
Cells(i, 2).Value = FoundNames(i)
Next
For i = 1 To 99
For j = i + 1 To 100
If FoundNames(i) FoundNames(j) Then
temp = FoundNames(j)
FoundNames(j) = FoundNames(i)
FoundNames(i) = temp
Debug.Print i, j, FoundNames(i), FoundNames(j)
End If
Next
Next
For i = 1 To 100
Cells(i, 1).Value = FoundNames(i)
Next
End Sub

but all the blanks will be at the bottom.

--
Regards,
Tom Ogilvy


"KR" wrote in message
...
I have an array (1 to 100) in memory (I'll never use more than 80-90, but
wanted some extra room just in case). I'm trying to use the following code
to sort the array into alphabetical order before adding the array to a
combobox, but it isn't working. I eliminated all blanks from my array, so
these are just random names in the array (my test set has about 40 names).
Of most importance, I want to sort these names in complete alpha order,

even
though the strings are of different length (I'm not sure if that matters,

or
how to accomodate if it does, but so far the names don't appear to be
sorting at all)

no matter how I look at it, I just can't figure out what I'm doing wrong.

For i = 99 To 1 Step -1
For j = (i + 1) To 1 Step -1
If FoundNames(i) FoundNames(j) Then
temp = FoundNames(j)
FoundNames(j) = FoundNames(i)
FoundNames(i) = temp
End If
Next
Next

I appreciate your help,
Keith

--
The enclosed questions or comments are entirely mine and don't represent

the
thoughts, views, or policy of my employer. Any errors or omissions are my
own.




  #3   Report Post  
Posted to microsoft.public.excel.programming
KR KR is offline
external usenet poster
 
Posts: 121
Default Request code help, sorting array in memory

Works perfectly- Thanks Tom!!
:-)
Keith

"Tom Ogilvy" wrote in message
...
If you won't have any blanks in the lower indexed end of the array, and

you
want your spaces at the upper indexed area of the array, you can do:

Sub SortArray()
Dim FoundNames(1 To 100) As String
For i = 1 To 40
For j = 1 To Int(Rnd() * 10 + 1)
FoundNames(i) = FoundNames(i) & Chr(Int(Rnd * 26 + 65))
Next
Next
For i = 1 To 100
Cells(i, 2).Value = FoundNames(i)
Next
For i = 1 To 99
For j = i + 1 To 100
If FoundNames(i) FoundNames(j) And _
FoundNames(j) < "" Then
temp = FoundNames(j)
FoundNames(j) = FoundNames(i)
FoundNames(i) = temp
Debug.Print i, j, FoundNames(i), FoundNames(j)
End If
Next
Next
For i = 1 To 100
Cells(i, 1).Value = FoundNames(i)
Next
End Sub

If you want it sorted without regards to blanks:

Sub SortArray()
Dim FoundNames(1 To 100) As String
For i = 1 To 40
For j = 1 To Int(Rnd() * 10 + 1)
FoundNames(i) = FoundNames(i) & Chr(Int(Rnd * 26 + 65))
Next
Next
For i = 1 To 100
Cells(i, 2).Value = FoundNames(i)
Next
For i = 1 To 99
For j = i + 1 To 100
If FoundNames(i) FoundNames(j) Then
temp = FoundNames(j)
FoundNames(j) = FoundNames(i)
FoundNames(i) = temp
Debug.Print i, j, FoundNames(i), FoundNames(j)
End If
Next
Next
For i = 1 To 100
Cells(i, 1).Value = FoundNames(i)
Next
End Sub

but all the blanks will be at the bottom.

--
Regards,
Tom Ogilvy


"KR" wrote in message
...
I have an array (1 to 100) in memory (I'll never use more than 80-90,

but
wanted some extra room just in case). I'm trying to use the following

code
to sort the array into alphabetical order before adding the array to a
combobox, but it isn't working. I eliminated all blanks from my array,

so
these are just random names in the array (my test set has about 40

names).
Of most importance, I want to sort these names in complete alpha order,

even
though the strings are of different length (I'm not sure if that

matters,
or
how to accomodate if it does, but so far the names don't appear to be
sorting at all)

no matter how I look at it, I just can't figure out what I'm doing

wrong.

For i = 99 To 1 Step -1
For j = (i + 1) To 1 Step -1
If FoundNames(i) FoundNames(j) Then
temp = FoundNames(j)
FoundNames(j) = FoundNames(i)
FoundNames(i) = temp
End If
Next
Next

I appreciate your help,
Keith

--
The enclosed questions or comments are entirely mine and don't represent

the
thoughts, views, or policy of my employer. Any errors or omissions are

my
own.






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
Request for a code FARAZ QURESHI Excel Discussion (Misc queries) 0 January 27th 08 02:59 PM
Out of Memory: Array Transpose TheVisionThing Excel Programming 10 May 1st 05 08:47 PM
Excel SQL.Request restricts data returned to array GTSA Excel Worksheet Functions 4 January 11th 05 12:49 AM
quick code request michelle Excel Programming 1 June 23rd 04 09:59 AM
Excel: Array & Memory EG[_2_] Excel Programming 0 September 1st 03 05:29 PM


All times are GMT +1. The time now is 07:03 AM.

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"