joharmenezes
Member
How do I sort an array of dates in vba? I need a sub through which I can pass an array of dates as an argument. The array will have dates stored as [format(A1,"dd-mmm-yy")]
Thanks
Thanks
there is no need for a sample workbook. Let's say suppose I have an array for example, ("1-Dec-12","31-Dec-13",21-Jun-11,1-May-06). How would you sort it?Without any sample workbook attached …
Hi, joharmenezes!
Simply sort them as numbers, as they're numeric values representing dates: Data tab, Sort and Filter group, Sort icon, Order by Column A, From older to recent, Accept.
But don't ever expect to get them sorted as you used a volatile function like RANDBETWEEN to generate them.
Regards!
Hi, joharmenezes!
If you want to do it the hard way it's up to you:
a) From here:
http://chandoo.org/forum/threads/simple-database-build.13673/#post-80859
check SortSelected procedure
b) From here:
http://chandoo.org/forum/threads/alphanumeric-sorting-solved.10611/#post-61366
check SortAColumn procedure
Regards!
Sub GetUniqueAndCount()
Dim d As Object, c As Range, k, tmp As String
Set d = CreateObject("scripting.dictionary")
'I will select the columns of dates
For Each c In Selection
tmp = Trim(c.Value)
If Len(tmp) > 0 Then
If Year(DateValue(Format(tmp, "dd-mmm-yy"))) = 2013 Then
d(tmp) = d(tmp) + 1
End If
End If
Next c
i = 0
ReDim ThisArray(UBound(d.keys)) As Date
For Each k In d.keys
ThisArray(i) = DateValue(Format(k, "dd-mmm-yy"))
i = i + 1
Next k
Sort (ThisArray)
End Sub
Sub Sort(arr() As Date)
Dim Temp As Date
Dim i As Long
Dim j As Long
For j = 2 To UBound(arr)
Temp = arr(j)
For i = j - 1 To 1 Step -1
If (arr(i) <= Temp) Then GoTo 10
arr(i + 1) = arr(i)
Next i
i = 0
10 arr(i + 1) = Temp
Next j
End Sub
Hi, joharmenezes!How do I sort an array of dates in vba? I need a sub through which I can pass an array of dates as an argument. The array will have dates stored as [format(A1,"dd-mmm-yy")]
Hi, joharmenezes!
The 1st wrong thing is what you posted when started the thread: you were asking for code to sort an array and you yet had one, working or not but you had. So people like me who spent a few minutes searching WTF code was, now have to work again to check your code. Next time please state clearly what you want to achieve, what you've done and where you're stumped... as it's clearly stated here:
http://chandoo.org/forum/forums/new-users-please-start-here.14/
http://chandoo.org/forum/threads/new-users-please-read.294/
""
How to get the Best Results at Chandoo.org
The 2nd wrong thing is that in the procedure "Sort(arr() As Date)" you're only using a For...Next loop instead of two (the outer from 1 to N-1, the inner from outer+1 to N). It's a matter of logic and algorithms beyond the scope of this post -and forums- but it won't ever work. Further reading if interested in:
- When drafting a question, try and lay out the question in a clear and concise way.
- Try and tell the readers what is the situation, what have you tried and what do you want to achieve.
- Don't leave out information more info is better
- For the best/fastest results, Upload a Sample File using the "Upload a File" button at the bottom of the page.
http://www.sorting-algorithms.com/
https://en.wikipedia.org/wiki/Sorting_algorithm
And the bible from Niklaus Wirth, Algorithms + Data Structures = Programs:
http://www.amazon.com/Algorithms-Structures-Prentice-Hall-Automatic-Computation/dp/0130224189
Regards!
PS: WTF stands for where the file... just in case.
there is no need for a sample workbook
I uploaded this workbook as the previous guy asked me to do it.
Well not my fault
(Do u remember "Can u spot the errors?")
Sub sort()
Dim avSource() As Variant
Dim alHolder() As Long
Dim albuffer() As Long
Dim rSource As Range
Dim rOP As Range
Set rSource = Sheets("sheet1").UsedRange
avSource = rSource
ReDim alHolder(1 To UBound(avSource))
ReDim albuffer(1 To UBound(avSource))
For i = 1 To UBound(avSource)
alHolder(i) = avSource(i, 1)
Next i
For i = 1 To UBound(alHolder)
albuffer(i) = Application.WorksheetFunction.Large(alHolder, i)
Debug.Print albuffer(i)
Next i
Set rOP = Range("f1").Resize(UBound(albuffer), 1)
rOP = Application.Transpose(albuffer)
rOP.NumberFormat = "m/d/yyyy"
End Sub