Is it possible to sort a table by entry modification date? Specifically, how to access from VBA the last modification time of an object?

2

I have a MS Word document where I accumulate and frequently modify certain data in a very long table. So I would like to sort the data by latest modification time in the rows. Of course if I add a column where I enter the modification time by hand this would work but I would like to avoid this if possible.

OK as the question has been put on hold with the request to make it more concrete, I am trying to modify it accordingly.

What I specifically want (and then I can do it myself) is this:

Since MS Word has features like change tracking, each modified portion of text must have its last modification time recorded in some form.

My question is then - how can I access this information from within a Visual Basic macro? I mean something like ThisTable.ThisCell.ModificationTime() or ThatTable.CurrentRow.LastModificationSessionTime() or something similar.

მამუკა ჯიბლაძე

Posted 2015-01-31T10:20:34.060

Reputation: 154

So when you modify an element you want it to move to the top of the table? Or you want it done historically? – Raystafarian – 2015-01-31T11:09:36.310

@Raystafarian I would like to sort it in various ways, including this one – მამუკა ჯიბლაძე – 2015-01-31T11:54:32.310

If you don't timestamp the entries, what are you planning to use to sort on? AFAIK, tables don't automatically record when each value is entered. – fixer1234 – 2015-02-01T00:44:03.343

@fixer1234 Word has Track Changes feature (in the Review tab), so it is definitely recorded somewhere. Anyway the Excel solution below (with automatic timestamping) would suit me, except it is for one column of data while I have several. – მამუკა ჯიბლაძე – 2015-02-01T05:56:27.253

I believe track changes works at the session level. So at one sitting, you go in and create or modify a bunch of rows and then save the result. All of those entries would be part of the same change. You wouldn't have a basis to sort by time within the set. – fixer1234 – 2015-02-01T07:15:30.353

@fixer1234 When looking at revisions, I see separate modification times for revisions of each chunk of text, and this is OK for me - if I can sort by most recent session at which the row has been modified that is all I want – მამუკა ჯიბლაძე – 2015-02-01T07:51:53.380

Answers

2

This is for an Excel worksheet. Data is entered or modified in column A. The macro automatically enters the date/time in column B. The macro then sorts column A & B by column B, putting the most recent at the top.

Enter the following event macro in the worksheet code area:

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim A As Range
    Dim AB As Range
    Set A = Range("A:A")
    Set AB = Range("A:B")

    If Intersect(Target, A) Is Nothing Then Exit Sub
    If Target.Count > 1 Then Exit Sub
    Application.EnableEvents = False
        Target.Offset(0, 1) = Now
        AB.Sort Key1:=Range("B1"), Order1:=xlDescending, Header:=xlNo, _
            OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
            DataOption1:=xlSortNormal
    Application.EnableEvents = True
End Sub

Because it is worksheet code, it is very easy to install and automatic to use:

  1. right-click the tab name near the bottom of the Excel window
  2. select View Code - this brings up a VBE window
  3. paste the stuff in and close the VBE window

If you have any concerns, first try it on a trial worksheet.

If you save the workbook, the macro will be saved with it. If you are using a version of Excel later then 2003, you must save the file as .xlsm rather than .xlsx

To remove the macro:

  1. bring up the VBE windows as above
  2. clear the code out
  3. close the VBE window

To learn more about macros in general, see:

http://www.mvps.org/dmcritchie/excel/getstarted.htm

and

http://msdn.microsoft.com/en-us/library/ee814735(v=office.14).aspx

To learn more about Event Macros (worksheet code), see:

http://www.mvps.org/dmcritchie/excel/event.htm

Macros must be enabled for this to work!

Gary's Student

Posted 2015-01-31T10:20:34.060

Reputation: 15 540

This is great! And so simple - I do not even need the piece of code from AB.Sort till ...=xlSortNormal since I prefer to sort it by modification time not immediately but only when needed. The only problem I cannot solve is this: if I have several columns and I want to react on change in any of them, how to modify the code? I can of course replace A by say AE and B by F, but what to do with the part Target.Offset(0,1)? – მამუკა ჯიბლაძე – 2015-01-31T19:34:54.617