+ Reply to Thread
Results 1 to 2 of 2

Using Module to Break Time Elapsed into two categories based on Interval

Hybrid View

staplegunz Using Module to Break Time... 12-12-2011, 01:01 PM
rylo Re: Using Module to Break... 12-12-2011, 09:45 PM
  1. #1
    Registered User
    Join Date
    12-12-2011
    Location
    Milwaukee, Wisconsin
    MS-Off Ver
    Excel 2007
    Posts
    4

    Using Module to Break Time Elapsed into two categories based on Interval

    Hello,

    The goal of this code is to take 4 time values, timeon, timeoff, peakstart, and peakend. These are time formatted cells in excell that will vary in actual time.
    Then, I have it count how many minutes of the appliance is on during peak hours.

    The end result I am looking for is for it to output the peak hours in the cell the function is in, and the nonpeak hours in the cell one column to the right, in number form, not time.

    I keep getting a #Value, but cannot figure out why or what I am doing wrong.

    Here is the main block of code
    Function onpeak(timeon, timeoff)
    Dim xtime As Double
    Dim min As Date
    Dim peakhrs As Date
    Dim offpeakhrs As Date
    Dim peakend As Double
    Dim peakstart As Double
    
    
    xtime = timeon
    min = Time(0, 0, 0)
    peakhrs = Time(0, 0, 0)
    offpeakhrs = Time(0, 0, 0)
        
    If peakstart <= timeon And peakend > timeon Then
    
            Do While xtime < pke And xtime <> timeoff
    
               xtime = xtime + Time(0, 1, 0)
      
               min = min + Time(0, 1, 0)
    
            Loop
    
        peakhrs = min * 24
        nonpeakhrs = Tdif(timeon, timeoff) - (peakhrs)
    
    Else
    
        peakhrs = Time(0, 0, 0) * 24
        nonpeakhrs = Tdif(timeon, timeoff)
    
    End If
    
    ActiveCell.Value = peakhrs
    ActiveCell.Offset(0, 1).Value = nonpeakhrs
    
    End Function
    Here is a function I call in that code.
    Function Tdif(time1, time2)
    
    If time1 > time2 Then
        Tdif = (time2 - time1 + 1) * 24
    Else
        Tdif = (time2 - time1) * 24
    End If
    
    End Function
    If you could help me out, that would be great.

    Mike

  2. #2
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591

    Re: Using Module to Break Time Elapsed into two categories based on Interval

    Hi

    How about an example workbook.

    As an aside, a function should not be used to put a value into a cell. It should return a result in the cell where the function resides. If you want to actively populate a value into a cell, then you should use a sub.

    rylo

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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