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
Here is a function I call in that 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
If you could help me out, that would be great.Function Tdif(time1, time2) If time1 > time2 Then Tdif = (time2 - time1 + 1) * 24 Else Tdif = (time2 - time1) * 24 End If End Function
Mike
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
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks