+ Reply to Thread
Results 1 to 2 of 2

Calculate time difference vba from selection

  1. #1
    Registered User
    Join Date
    10-06-2012
    Location
    JHB
    MS-Off Ver
    Excel 2007
    Posts
    8

    Calculate time difference vba from selection

    Hi all,

    I have just recently started with vba coding
    and now I'm hooked

    I am busy with a work project to calculate downtime
    Where a user can select multiple cells with time
    format hh:mm and calculate the time difference
    Via a command button and return the downtime
    value in minutes to a target cell within the same
    sheet

    Thanks in advance

  2. #2
    Registered User
    Join Date
    10-06-2012
    Location
    JHB
    MS-Off Ver
    Excel 2007
    Posts
    8

    Re: Calculate time difference vba from selection

    Quote Originally Posted by aka 42 View Post
    Hi all,

    I have just recently started with vba coding
    and now I'm hooked

    I am busy with a work project to calculate downtime
    Where a user can select multiple cells with time
    format hh:mm and calculate the time difference
    Via a command button and return the downtime
    value in minutes to a target cell within the same
    sheet

    Thanks in advance
    I am using this code:

    Sub SeriesDifferentiation()

    Dim MyRange As Object
    Dim LeftCol As Integer, RightCol As Integer
    Dim BeginRow As Long, EndRow As Long
    Dim y As Integer

    Set MyRange = Application.InputBox(prompt:="Select a range", Type:=8)
    LeftCol = MyRange(1, 1).column
    RightCol = MyRange(1, MyRange.Columns.Count).column
    BeginRow = MyRange(1, 1).Row
    EndRow = MyRange(MyRange.Rows.Count, 1).Row

    If Selection Is Nothing Then
    Exit Sub

    Else
    Set MyDestinationRange = Application.InputBox(prompt:="Click on any cell of the destination column", Type:=8)
    DestinationColumn = MyDestinationRange(1, 1).column

    End If

    If LeftCol <> RightCol Then
    MsgBox "Must be a unique column!"
    Exit Sub

    End If

    For y = BeginRow + 1 To EndRow
    If (IsEmpty(Cells(y, LeftCol)) = False) And (IsEmpty(Cells(y - 1, LeftCol)) = False) Then
    Cells(y, DestinationColumn).Value = Cells(y, LeftCol).Value - Cells(y - 1, LeftCol).Value

    Else
    Cells(y, DestinationColumn).Value = " "

    End If

    Next y

    End Sub

    I would like to calculate time after 00:00 and change the selection so that the user can select cells instead.
    Also I would like the downtime to be automatically calculated and entered to
    target cell b42 in minutes.

    Thanks again!

+ 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