A Microsoft Office (Excel, Word) forum. OfficeFrustration

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » OfficeFrustration forum » Microsoft Word » Tables
Site Map Home Register Authors List Search Today's Posts Mark Forums Read  

Sorting by dates



 
 
Thread Tools Display Modes
  #1  
Old December 3rd, 2009, 09:02 PM posted to microsoft.public.word.tables
Asking a question
external usenet poster
 
Posts: 1
Default Sorting by dates

One column of my table contains dates, in different forms. Some are
01/01/2002, 01/2002 or just 2002. I understand it may separate these dates,
but suddenly it's not sorting most of the table correctly, with 01/01/2002
and 03/03/2003 in the wrong order and other dates thrown in. How do I fix
this?
  #2  
Old December 4th, 2009, 03:47 AM posted to microsoft.public.word.tables
Doug Robbins - Word MVP
external usenet poster
 
Posts: 8,239
Default Sorting by dates

If the dates are in the first column and your table has a header row, the
following macro will sort them correctly:

Dim sortcolumn As Column
Dim i As Long
Dim ddate As Range
With Selection.Tables(1)
Set sortcolumn = .Columns.Add(.Columns(1))
For i = 2 To .Rows.Count
Set ddate = .Cell(i, 2).Range
ddate.End = ddate.End - 1
If Len(ddate.Text) = 4 Then
.Cell(i, 1).Range.Text = "01/01/" & ddate.Text
ElseIf Len(ddate.Text) = 7 Then
.Cell(i, 1).Range.Text = "01/" & ddate.Text
Else
.Cell(i, 1).Range.Text = ddate.Text
End If
Next i
.SortAscending
.Columns(1).Delete
End With


--
Hope this helps,

Doug Robbins - Word MVP

Please reply only to the newsgroups unless you wish to obtain my services on
a paid professional basis.

"Asking a question" Asking a wrote in
message ...
One column of my table contains dates, in different forms. Some are
01/01/2002, 01/2002 or just 2002. I understand it may separate these
dates,
but suddenly it's not sorting most of the table correctly, with 01/01/2002
and 03/03/2003 in the wrong order and other dates thrown in. How do I fix
this?


 




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT +1. The time now is 08:04 AM.


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