+ Reply to Thread
Results 1 to 2 of 2

Using VBA to determine a timespan

Hybrid View

  1. #1
    Registered User
    Join Date
    06-19-2015
    Location
    Michigan
    MS-Off Ver
    2010
    Posts
    30

    Using VBA to determine a timespan

    Hello,

    I am trying to work out a bit of code that looks at a cell (formatted as time, preferably "h:mm:ss;@") and will then output a number in another cell if that time is within a timespan.

    This is what I want to happen:
    Time Charge
    0:15:00 1
    0:46:00 2
    1:15:01 3

    The time is already determined with a formula (=E4-C4)

    The timespans are: between 0:00:00 and 0:30:00, between 0:30:01 and 1:00:00, between 1:00:01 and 1:30:00, and so on by half-hour increments until it reaches 2:30:00.

    What I have currently is this:
    Dim gcell As Range, fcell As Range
    Dim cel As Range, cels As Range
    Set gcell = Range("g4:g70")
    Set fcell = Range("F4:F70")
    
    For Each cel In gcell
        For Each cels In fcell
            If cels.Value > "0:00:00" And cels.value < "0:30:00" Then
                cel = 1
            ElseIf cels.value > "0:30:01" And cels.Value < "1:00:00" Then
                cel = 2
            Else
                cel = 0
            End If
        Next
    Next
    This is not working at all and I do not know what I'm doing wrong. I know I could have this as a formula within the cell but I would like to avoid that if at all possible.

  2. #2
    Registered User
    Join Date
    06-19-2015
    Location
    Michigan
    MS-Off Ver
    2010
    Posts
    30

    Re: Using VBA to determine a timespan

    I figured it out! There is a function called TIMEVALUE which did exactly what I needed. Here is the updated code:

    Dim gcell As Range
    Dim cel As Range
    Set gcell = Range("g4:g70")
    
    For Each cel In gcell
            If cel.Offset(0, -1) > TimeValue("1:00:00") And cel.Offset(0, -1) < TimeValue("1:30:00") Then
                cel = 1
            ElseIf cel.Offset(0, -1) > TimeValue("1:30:01") And cel.Offset(0, -1) < TimeValue("2:00:00") Then
                cel = 2
            ElseIf cel.Offset(0, -1) > TimeValue("2:00:01") And cel.Offset(0, -1) < TimeValue("2:30:00") Then
                cel = 3
            End If
    Next

+ 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. [SOLVED] Formula to calculate: # of hours in a timespan that fall between Midnight and 6AM
    By Deicidium in forum Excel Formulas & Functions
    Replies: 15
    Last Post: 07-23-2015, 05:05 PM
  2. [SOLVED] Determine if last row is odd or even
    By kosherboy in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-31-2014, 02:36 PM
  3. Replies: 6
    Last Post: 10-01-2012, 10:52 AM
  4. Timespan formatting
    By eldidd in forum Excel General
    Replies: 5
    Last Post: 10-08-2008, 05:33 AM
  5. How to Determine 1st, 2nd & 3rd for a PWD
    By Pete n PWD Land in forum Excel General
    Replies: 3
    Last Post: 02-18-2005, 10:06 AM
  6. [SOLVED] Determine 1st 2nd 3rd in PWD
    By Pete n PWD Land in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-16-2005, 04:52 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