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.

6
  • So when you modify an element you want it to move to the top of the table? Or you want it done historically? Commented Jan 31, 2015 at 11:09
  • @Raystafarian I would like to sort it in various ways, including this one Commented Jan 31, 2015 at 11:54
  • 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
    Commented Feb 1, 2015 at 0:44
  • @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. Commented Feb 1, 2015 at 5:56
  • 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
    Commented Feb 1, 2015 at 7:15

1 Answer 1

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!

1
  • 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)? Commented Jan 31, 2015 at 19:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .