+ Reply to Thread
Results 1 to 3 of 3

Determine difference in time

Hybrid View

lacke81c Determine difference in time 10-20-2014, 11:43 AM
LJMetzger Re: Determine difference in... 10-20-2014, 04:09 PM
lacke81c Re: Determine difference in... 10-20-2014, 06:12 PM
  1. #1
    Registered User
    Join Date
    09-11-2014
    Location
    San Francisco, CA
    MS-Off Ver
    2013
    Posts
    24

    Determine difference in time

    Hello,
    I’m using VBA to run a code for an excel spreadsheet. My spreadsheet has four columns (DATE, TIME, USER, ACTION) and about 400 rows.

    I’d like my code to determine if there was less than 4 minutes between actions for a user. If it determines that this is true, then all of the text in the “older time” in those 4 columns making up that row should change to red.

    EXAMPLE:
    (1) 9/30/14 21:56 32 IN
    (2) 9/29/14 20:45 28 IN
    (3) 9/29/14 20:44 28 IN
    (4) 9/28/14 19:12 31 IN
    (5) 9/28/14 19:10 27 IN
    (6) 9/27/14 12:36 28 IN
    (7) 9/26/14 12:34 28 IN

    -Row 3 should change to red text in that row because the time difference is less than 4 minutes and it is the same user (#28) and it is the ‘older’ time

    -Row 5 does not change to red text – even though the time difference is less than 4 minutes, they are not the same user

    -Row 7 does not change to red text – even though the time difference is less than 4 minutes, and it is the same user, the action occurred on a different day

    Any help and shared expertise would be greatly appreciated! Thank you!

  2. #2
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Determine difference in time

    Hi lacke81c and welcome to ExcelForum,

    When I tested the following macro, the data started in cell "A1" (date).
    Option Explicit
    
    Sub ProcessItemsFromAList()
    
      Const nFileListHeaderROW = 0
      Const FourMinutesWorthOfSEONDS = 4 * 60
      
      Dim wb As Workbook
      Dim ws As Worksheet
    
      Dim myDeltaSeconds As Long
      Dim myDate As Date
      Dim myTime As Date
      Dim myDateAndTime As Date
      Dim myDateAndTimePrevious As Date
    
      Dim iCount As Long
      Dim iError As Long
      Dim iErrorCount As Long
      Dim iOutputRow As Long
      Dim iRow As Long
      
      Dim bNeedMore As Boolean
      
      Dim sAction As String
      Dim sDate As String
      Dim sTime As String
      Dim sUser As String
      Dim sUserPrevious As String
      
      Set wb = ThisWorkbook
      Set ws = wb.ActiveSheet
      
      
      'Initialize the Source Row
      iRow = nFileListHeaderROW
    
      
      'Loop until all matching files have been found
      bNeedMore = True
      While bNeedMore
        'Debug.Print sPath & sFileName
        
        'Increment the source row number
        iRow = iRow + 1
        
        'Get the data from the Row
        sDate = ws.Cells(iRow, "A").Text
       
        'Terminate if 'File Name' is blank
        If Len(sDate) = 0 Then
          bNeedMore = False
        Else
       
          myDate = 0
          myTime = 0
          myDateAndTime = 0
          sTime = ws.Cells(iRow, "B").Text
          sUser = ws.Cells(iRow, "C").Text
          sAction = ws.Cells(iRow, "D").Text
          
          If IsDate(sDate) Then
            myDate = CDate(sDate)
          End If
        
          If IsDate(sTime) Then
            myTime = CDate(sTime)
          End If
        
          myDateAndTime = myDate + myTime
        
          Debug.Print iRow, myDateAndTime, sUser  'Write to Debugger Immediate Window (CTRL G)
          
          If sUser = sUserPrevious Then
          
            myDeltaSeconds = DateDiff("s", myDateAndTime, myDateAndTimePrevious)
            Debug.Print "---------", myDeltaSeconds
            If myDeltaSeconds < FourMinutesWorthOfSEONDS Then
              Debug.Print "RED"
              ws.Cells(iRow, "A").Interior.Color = RGB(255, 0, 0) 'Red
              ws.Cells(iRow, "B").Interior.Color = RGB(255, 0, 0) 'Red
              ws.Cells(iRow, "C").Interior.Color = RGB(255, 0, 0) 'Red
              ws.Cells(iRow, "D").Interior.Color = RGB(255, 0, 0) 'Red
            End If
            
          End If
          
          myDateAndTimePrevious = myDateAndTime
          sUserPrevious = sUser
        End If
          
      Wend
      
      'Clear object pointers
      Set wb = Nothing
      Set ws = Nothing
      
    End Sub
    Lewis

  3. #3
    Registered User
    Join Date
    09-11-2014
    Location
    San Francisco, CA
    MS-Off Ver
    2013
    Posts
    24

    Re: Determine difference in time

    Thank you LJMetzger !

    It is working perfectly

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. formula to determine what time double time starts and ends after 16:30
    By cfinch100 in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 10-22-2013, 12:05 PM
  2. Replies: 21
    Last Post: 11-30-2012, 07:28 PM
  3. Replies: 3
    Last Post: 01-24-2011, 12:48 PM
  4. [SOLVED] Can you compare similar spreadsheets to determine a difference?
    By DLopez79 in forum Excel General
    Replies: 1
    Last Post: 01-09-2006, 05:40 PM
  5. How to determine difference?
    By Fionavar in forum Excel General
    Replies: 7
    Last Post: 01-21-2005, 01:06 PM

Tags for this Thread

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