PDA

View Full Version : Help With Sorting



zoom38
03-27-2007, 02:14 PM
I have a sub with 10 variables. I can put the values on the worksheet and then sort them but I would like to be able to sort the variables in code without putting the values on the worksheet. I have been researching it and it appears I have to build and array. I don't have any experience with arrays nor do I understand them. Can some one please assist me.


Sub ColumnY()

Dim LastRow As Long
Dim First As Long
Dim Second As Long
Dim Third As Long
Dim Fourth As Long
Dim Fifth As Long
Dim Sixth As Long
Dim Seventh As Long
Dim Eighth As Long
Dim Ninth As Long
Dim None As Long
Dim LR As Long
Dim r As Long

'Find The Last Used Cell In Column O
LastRow = Cells(Rows.Count, 15).End(xlUp).Row
r = 3
Do
If Cells(r, 15).Value = "1st" Then
First = First + 1
End If

If Cells(r, 15).Value = "2nd" Then
Second = Second + 1
End If

If Cells(r, 15) = "3rd" Then
Third = Third + 1
End If

If Cells(r, 15).Value = "4th" Then
Fourth = Fourth + 1
End If

If Cells(r, 15).Value = "5th" Then
Fifth = Fifth + 1
End If

If Cells(r, 15).Value = "6th" Then
Sixth = Sixth + 1
End If

If Cells(r, 15).Value = "7th" Then
Seventh = Seventh + 1
End If

If Cells(r, 15).Value = "8th" Then
Eighth = Eighth + 1
End If

If Cells(r, 15).Value = "9th" Then
Ninth = Ninth + 1
End If

If Cells(r, 15).Value = "None" Then
None = None + 1
End If

r = r + 1
Loop While r <= LastRow
End Sub



Thanks
Gary

mdmackillop
03-27-2007, 02:56 PM
Hi Gary,
Can you post your workbook?

Bob Phillips
03-27-2007, 03:08 PM
What is it that needs sorting, and where is the array?

zoom38
03-27-2007, 03:26 PM
MD I attached the file Im playing with. Module 8 is where the variables are.

XLD I want the variables First through Ninth sorted. I had to paste the values to the worksheet and then sort them. I was hoping to sort them in code without pasting them to the worksheet.

Thanks

johnske
03-27-2007, 05:02 PM
... I was hoping to sort them in code without pasting them to the worksheet.

Thanks:dunno what's the point of doing it that way? Excels inbuilt sort is just so much faster than any VBA method

zoom38
03-27-2007, 08:29 PM
It is a way to keep unnecessary data off of the worksheet. I did it the easy way and now I would like to learn more on doing it the other way. But it really isn't that important.

thank you

johnske
03-27-2007, 08:42 PM
It is a way to keep unnecessary data off of the worksheet. I did it the easy way and now I would like to learn more on doing it the other way. But it really isn't that important.

thank youHave a look here (http://vbaexpress.com/forum/showthread.php?t=11630) at xld's 'quicksort' then to get the idea (I think it's post #6) - which shows how to sort a column

Bob Phillips
03-28-2007, 10:19 AM
You will still need toi load the array YLP. Are you okay with that or do you need help? If the latter, is the size known in advance or only as you go through the data.

zoom38
03-29-2007, 11:10 AM
Thanks Johske & XLD. This is a file that Im playing around with. I have to work on another file that hopefully will only take a week so Im putting this one aside. I will reply when I get back to it and let you know how it went.

Thanks Again
Gary