+ Reply to Thread
Results 1 to 17 of 17

Reverse Data Within Cell By Date

Hybrid View

  1. #1
    Registered User
    Join Date
    01-09-2009
    Location
    US
    MS-Off Ver
    Excel 2003
    Posts
    10

    Reverse Data Within Cell By Date

    Greetings,

    I have attached an Excel spreadsheet and I will apologize now if I lose you. This spreadsheet contains two entries:

    "SwapIt" works great but will only reverse the most current date entry and list it first in the cell and the remaining date entries will remain in oldest to latest dates.

    "ReverseDates" works great but errors out with <subscript out of range> when a manual date is entered in the notes (see system created format).

    System created format; the entries within a cell will always be - MM/DD/YYYY_tt:tt:tt_AM(PM)_username (this varies) followed by the notes entered by user. So in a nutshell, the system will generate all the following but from oldest dates to latest dates toward the bottom of the cell. I would like to achieve the reverse order, latest entries to oldest entries.

    Again my apologies for rambling.

    Regards!!
    Attached Files Attached Files

  2. #2
    Forum Contributor
    Join Date
    01-21-2005
    Location
    Colorado
    MS-Off Ver
    2000,2003,2007
    Posts
    481
    Did you look at why your code was throwing the error?
    When I tested it the problem was in cell H74.
    As part of the note the text has the date,time,username. But then in the text of the note there is another partial date "implementaion 1/09".
    It looks like your check
    If IsDate(SplitCell(i)) Then
    is improperly identifying this as a date and then hanging up on the
    MessageDate = SplitCell(i) & " " & SplitCell(i + 1) & _
                                " " & SplitCell(i + 2)
    line.

    Perhaps you could make the
    If IsDate(SplitCell(i)) Then
    more particular and have it check SplitCell(i) for the Date,time stamp that starts each new entry.

    HTH

  3. #3
    Registered User
    Join Date
    01-09-2009
    Location
    US
    MS-Off Ver
    Excel 2003
    Posts
    10
    Thank you HTH. However, I kind of inherited this form and was looking for some expertise as I am new to vb...

    I attempted adding your code and it still fails. I believe I am doing something wrong.

    Thanks again!!

  4. #4
    Forum Contributor
    Join Date
    01-21-2005
    Location
    Colorado
    MS-Off Ver
    2000,2003,2007
    Posts
    481
    I didn't give you any code to add to your project.

    I simply stepped through your code and identified where it was giving you the error. try using F8 and the debugger to identify where your code is giving you trouble.

    My suggestion was to either eliminate any text that is identified as a date by the line.
    If IsDate(SplitCell(i)) Then
    Or to modify that line to discriminate between a real date and a date in the body of the text.

  5. #5
    Forum Contributor
    Join Date
    01-09-2009
    Location
    Cedar Hill, Tx
    MS-Off Ver
    Excel 2003
    Posts
    200
    I can't help with your request but can maybe give you a direction as an old database guy. If you can strip the DTG and userName data from the cell and put them into their own cell on the record, you would have a lot more flexibility with data sort, filtering, etc. and it should be easier, too. ;-> FWIW.

  6. #6
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258
    Hello nobi,

    I have added 2 new macro to the attached workbook. The code code for these appears below. This will reverse the data order of each log entry in the cell, bold the date, and add a vertical tab to each log entry. You can easily change where the log entries are located. The worksheet name and starting cell address are located in the macro ReverseAllDates and are marked in red.
    Sub ReverseDates2(ByRef LogCell As Range)
    
      Dim LenArray() As Integer
      Dim LogArray() As String
      Dim LogData As String
      Dim Matches As Variant
      Dim I As Integer, N As Integer
      Dim RE As Object
      Dim X As Long
      
        Set RE = CreateObject("VBScript.RegExp")
        
          With RE
            .Global = True
            .Pattern = "(\d{2}/){2}\d{4}\s(\d{1,2}:){2}\d{2}\s[AP][M]"
          End With
          
         'Place log entries in a string variable for faster access
          LogData = LogCell.Value
         'Remove all vertical tabs
          LogData = Replace(LogData, vbLf, "")
          
          If RE.Test(LogData) = True Then
             Set Matches = RE.Execute(LogData)
             N = Matches.Count - 1
            'Array holds each log entry
             ReDim LogArray(N)
            'Array holds the character lengths of the date and log entry
             ReDim LenArray(N, 1)
               With Matches
                 For I = 0 To N
                   If I < N Then
                      LogArray(I) = Mid(LogData, .Item(I).FirstIndex + 1, .Item(I + 1).FirstIndex + 1 - (.Item(I).FirstIndex + 1))
                      LenArray(I, 1) = .Item(I).Length
                   End If
                 Next I
                 LogArray(N) = Mid(LogData, .Item(N).FirstIndex + 1, Len(LogData) - .Item(N).FirstIndex + 1)
                 LenArray(N, 1) = .Item(N).Length
              End With
          
           'Reset the cell
            LogCell = ""
            LogCell.Font.FontStyle = "regular"
            
           'Load the log entires in reverse and add a vertical tab to each line
            For I = N To 0 Step -1
              LenArray(I, 0) = Len(LogCell) + 1
              LogCell = LogCell & LogArray(I) & vbLf
            Next I
          
           'Bold the entry dates - This extra loop is necessary because the
           'Font Style of the cell is set by the first character. The style is
           're-applied when the cell's characters are concatenated.
            For I = 0 To N
              LogCell.Characters(LenArray(I, 0), LenArray(I, 1)).Font.Bold = True
            Next I
          End If
            
       'Free Object in Memory
        Set RE = Nothing
        
    End Sub
    
    
    Sub ReverseAllDates()
    
      Dim Col As Long
      Dim Cell As Range
      Dim LastRow As Long
      Dim Rng As Range
      Dim StartCell As Range
      Dim StartRow As Long
      Dim Wks As Worksheet
      
        Set Wks = Worksheets("Intl Report CR")
        Set StartCell = Wks.Range("H2")
        
          With Wks
            Col = StartCell.Column
            StartRow = StartCell.Row
            LastRow = .Cells(Rows.Count, StartCell.Column).End(xlUp).Row
            LastRow = IIf(LastRow < StartRow, StartRow, LastRow)
            Set Rng = .Range(.Cells(StartRow, Col), .Cells(LastRow, Col))
          End With
          
          For Each Cell In Rng
            ReverseDates2 Cell
          Next Cell
      
    End Sub
    Sincerely,
    Leith Ross
    Attached Files Attached Files
    Last edited by Leith Ross; 01-10-2009 at 08:20 PM. Reason: Added code line to free regular expression in memory

  7. #7
    Registered User
    Join Date
    01-09-2009
    Location
    US
    MS-Off Ver
    Excel 2003
    Posts
    10
    Hi Leith,

    You are my hero!! Works great as desired. A million thanks for the assistance. Is it possible to bold the user name as well?

    Thanks again!!!!!!
    Last edited by nobi; 01-12-2009 at 07:31 PM.

  8. #8
    Registered User
    Join Date
    01-09-2009
    Location
    US
    MS-Off Ver
    Excel 2003
    Posts
    10
    Thanks Leith!! If it helps any, the user name will always be uppercase. Then all notes are always entered as proper sentence case.

  9. #9
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258
    Hello nobi,

    It looks like the user name also follows the time stamp with a space between them. I'm hoping the are no user names with spaces in them.

    Sincerely,
    Leith Ross

  10. #10
    Registered User
    Join Date
    01-09-2009
    Location
    US
    MS-Off Ver
    Excel 2003
    Posts
    10
    Hi Leith,

    Its the date/time stamp with "AM" OR "PM", space and then the user name with no users ever having a space in within the user name.

    Thank again for your efforts. Its greatly appreciated.
    Last edited by nobi; 01-13-2009 at 02:26 PM.

  11. #11
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258
    Hello nobi,

    Got it to work. Here is the revised code which is already been added in the attached workbook.
    Sub ReverseDates2(ByRef LogCell As Range)
    
      Dim LenArray() As Integer
      Dim LogArray() As String
      Dim LogData As String
      Dim Matches As Variant
      Dim I As Integer, N As Integer
      Dim RE As Object
      Dim X As Long
      Dim ZZ As Variant
      
        Set RE = CreateObject("VBScript.RegExp")
        
          With RE
            .Global = True
            .Pattern = "(\d{2}/){2}\d{4}\s(\d{1,2}:){2}\d{2}\s[AP][M]\s\w+\s"
          End With
          
         'Place log entries in a string variable for faster access
          LogData = LogCell.Value
         'Remove all vertical tabs
          LogData = Replace(LogData, vbLf, "")
          
          If RE.Test(LogData) = True Then
             Set Matches = RE.Execute(LogData)
             N = Matches.Count - 1
            'Array holds each log entry
             ReDim LogArray(N)
            'Array holds the character lengths of the date and log entry
             ReDim LenArray(N, 1)
               With Matches
                 For I = 0 To N
                   If I < N Then
                      LogArray(I) = Mid(LogData, .Item(I).FirstIndex + 1, .Item(I + 1).FirstIndex + 1 - (.Item(I).FirstIndex + 1))
                      LenArray(I, 1) = .Item(I).Length
                   End If
                   ZZ = Matches(I)
                 Next I
                 LogArray(N) = Mid(LogData, .Item(N).FirstIndex + 1, Len(LogData) - .Item(N).FirstIndex + 1)
                 LenArray(N, 1) = .Item(N).Length
              End With
          
           'Reset the cell
            LogCell = ""
            LogCell.Font.FontStyle = "regular"
            
           'Load the log entires in reverse and add a vertical tab to each line
            For I = N To 0 Step -1
              LenArray(I, 0) = Len(LogCell) + 1
              LogCell = LogCell & LogArray(I) & vbLf
            Next I
          
           'Bold the entry dates - This extra loop is necessary because the
           'Font Style of the cell is set by the first character. The style is
           're-applied when the cell's characters are concatenated.
            For I = 0 To N
              LogCell.Characters(LenArray(I, 0), LenArray(I, 1) - 1).Font.Bold = True
            Next I
          End If
            
       'Free Object in Memory
        Set RE = Nothing
        
    End Sub
    
    Sub ReverseAllDates()
    
      Dim Col As Long
      Dim Cell As Range
      Dim LastRow As Long
      Dim Rng As Range
      Dim StartCell As Range
      Dim StartRow As Long
      Dim Wks As Worksheet
      
        Set Wks = Worksheets("Intl Report CR")
        Set StartCell = Wks.Range("H2")
        
          With Wks
            Col = StartCell.Column
            StartRow = StartCell.Row
            LastRow = .Cells(Rows.Count, StartCell.Column).End(xlUp).Row
            LastRow = IIf(LastRow < StartRow, StartRow, LastRow)
            Set Rng = .Range(.Cells(StartRow, Col), .Cells(LastRow, Col))
          End With
          
          Application.ScreenUpdating = False
            For Each Cell In Rng
              ReverseDates2 Cell
            Next Cell
          Application.ScreenUpdating = True
          
    End Sub
    Sincerely,
    Leith Ross
    Attached Files Attached Files

  12. #12
    Registered User
    Join Date
    01-09-2009
    Location
    US
    MS-Off Ver
    Excel 2003
    Posts
    10
    Hi Leith,

    Need I say more... thank you for your great work and assistance.

    It is appreciated!!!

    Regards!!!!

  13. #13
    Registered User
    Join Date
    01-09-2009
    Location
    US
    MS-Off Ver
    Excel 2003
    Posts
    10
    Hi Leith,

    I hope Im not pushing my luck, but do you know if I can restrict (delete) to only show 4 date/user entries. The work long is getting kind of long with all the notes entered, so I wanted to see the 4 most recent entries.

    Thanks again!!

  14. #14
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258
    Hello nobi,

    I have added an If...Then to limit the maximum entries to 4. This will of courser permanently change the data on the sheet. You will lose the other entries above 4 for that cell.
    Sub ReverseDates2(ByRef LogCell As Range)
    
      Dim LenArray() As Integer
      Dim LogArray() As String
      Dim LogData As String
      Dim Matches As Variant
      Dim I As Integer, N As Integer
      Dim RE As Object
      Dim X As Long
      
        Set RE = CreateObject("VBScript.RegExp")
        
          With RE
            .Global = True
            .Pattern = "(\d{2}/){2}\d{4}\s(\d{1,2}:){2}\d{2}\s[AP][M]"
          End With
          
         'Place log entries in a string variable for faster access
          LogData = LogCell.Value
         'Remove all vertical tabs
          LogData = Replace(LogData, vbLf, "")
          
          If RE.Test(LogData) = True Then
             Set Matches = RE.Execute(LogData)
             N = Matches.Count - 1
            'Array holds each log entry
             ReDim LogArray(N)
            'Array holds the character lengths of the date and log entry
             ReDim LenArray(N, 1)
               With Matches
                 For I = 0 To N
                   If I < N Then
                      LogArray(I) = Mid(LogData, .Item(I).FirstIndex + 1, .Item(I + 1).FirstIndex + 1 - (.Item(I).FirstIndex + 1))
                      LenArray(I, 1) = .Item(I).Length
                   End If
                 Next I
                 LogArray(N) = Mid(LogData, .Item(N).FirstIndex + 1, Len(LogData) - .Item(N).FirstIndex + 1)
                 LenArray(N, 1) = .Item(N).Length
              End With
          
           'Reset the cell
            LogCell = ""
            LogCell.Font.FontStyle = "regular"
            
           'Limit the entries to more than 4 per cell
            If N > 4 Then N = 4
    
           'Load the log entires in reverse and add a vertical tab to each line
            For I = N To 0 Step -1
              LenArray(I, 0) = Len(LogCell) + 1
              LogCell = LogCell & LogArray(I) & vbLf
            Next I
          
           'Bold the entry dates - This extra loop is necessary because the
           'Font Style of the cell is set by the first character. The style is
           're-applied when the cell's characters are concatenated.
            For I = 0 To N
              LogCell.Characters(LenArray(I, 0), LenArray(I, 1)).Font.Bold = True
            Next I
          End If
            
       'Free Object in Memory
        Set RE = Nothing
        
    End Sub
    Sincerely,
    Leith Ross

  15. #15
    Registered User
    Join Date
    01-09-2009
    Location
    US
    MS-Off Ver
    Excel 2003
    Posts
    10
    Hi Leith,

    Wow... thanks again!!!! All your help was/ is greatly appreciated!!!! I owe you lunch.

    Regards!!!

  16. #16
    Registered User
    Join Date
    01-09-2009
    Location
    US
    MS-Off Ver
    Excel 2003
    Posts
    10
    Hi Leith,

    I just wanted to mention one more thing if I may... the >4 is not saving the most recent entries. I moved the code and then it doesnt work?

    Your assistance is requested one more time if I may.

    Thanks!!!!!!!!!!!!!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1