Excel - Count TIme Cells - excel

I want to count number of cells containing the value time. Eg: 05:29, 14:36, 22:05.
Here, these times are entered in indiviudal cells. And I want to count number of cells containing time. Like Time: 3. But not the time function or total time function
-Thanks

Assuming your data is arranged like this
you can use =COUNTIF(B1:L1,">0") to count the non empty cells, and you will get then this
If you want to check for late arrivals, use this formula = IF(Q2<>"",IF(Q2-Z2 >0,"L","N"),"") where I called Q2 the cell containing the arrival time and Z2 the cell with the expected arrival time. You will get a L for late arrivals, a N for an arrival on time.

Another solution along the same lines as L.Dutch is to use the IsNumber() function of excel. It requires two steps. First using that function and then summing the TRUE values. The one advantage of this method is that it makes it more explicit as to what it will add up.
Thought I'd provide this in case it is useful, but I would use L. Dutch's answer first.

Related

Excel: Make a dynamic formula that counts a specified max sum of X consecutive days

I am trying to make a formula that could count the max sum of any number of consecutive days that I indicate in some cell. Here is the dataset and the formula:
Dataset
The formula that calculates the maximum sum of three consecutive days:
=MAX(IFERROR(INDEX(
INDEX(E2:AI2,0)+
INDEX(F2:AI2,0)+
INDEX(G2:AI2,0),
0),""))
As you can see the number of days here is determined by the number of rows in the formula that start with "Index". The only difference between these rows is the letters (E, F, G). Is there any way I could reference a cell in which I could put a number for those days, instead of adding more rows to this formula?
Another approach avoding use of Offset is to use Scan to generate an array of running totals, then subtract totals which are N elements apart (where N is the number of consecutive cells to be added):
=LET(range,E2:AI2,
length,A1,
runningTotal,SCAN(0,range,LAMBDA(a,b,a+b)),
sequence1,SEQUENCE(1,COLUMNS(range)-length+1,A1),
sequence2,SEQUENCE(1,COLUMNS(range)-length+1,0),
difference,INDEX(runningTotal,sequence1)-IF(sequence2,INDEX(runningTotal,sequence2),0),
MAX(difference))
The answer here was posted by another user on another website, so I will repost it here:
One way to achieve this without relying on a VBA solution would be to use the BYCOL() function (available for Excel for Microsoft 365):
=BYCOL(array, [function])
The array specifies the range to which you want to apply your function, and the function itself is specified in a lambda statement. In the end, you want to get the minimum value of the sum of x consecutive days. Assuming that your data is stored in the range E2:AI2 and the number of consecutive days is stored in cell A1, the function looks like this:
=MIN(BYCOL(E2:AI2,LAMBDA(col,SUM(OFFSET(col,,,,A1)))))
The MIN() part ensures that you get only the smallest sum of the array (all sums of the x consecutive values) returned. The array is simply the range in which your data is stored; it is named in the lambda argument col and consequently used by its name. In your case, you want to apply the sum function for, e.g., x = 4 consecutive days (where 4 is stored in cell A1).
However, with this simple specification, you run into the problem of offsetting beyond cells with values toward the right end of the data. This means that the last sum you get would be 81.8 (value on 31 Jan) + 3 times 0 because the cells are empty. To avoid this, you can combine your function with an IF() statement that replaces the result with an empty cell if the number of empty cells is greater than 0. The adjusted formula looks like this:
=MIN(BYCOL(E2:AI2,
LAMBDA(col,IF(COUNTIF(OFFSET(col,,,,A1),"")>0,"",SUM(OFFSET(col,,,,A1))))))
If you do not have the Microsoft 365 version, there are two approaches that would also work. However, the two approaches are a bit more tedious, especially for cases with multiple days (because the number of days can not really be set automatically; except for potentially constructing the ranges with a combination of ADDRESS() and INDIRECT()), but I would still argue a bit neater than your current specification:
=MIN(INDEX(E2:AF2+F2:AG2+G2:AH2+H2:AI2,0))
=SUMPRODUCT(MIN(E2:AF2+F2:AG2+G2:AH2+H2:AI2))
The idea regarding the ranges is the same in both scenarios, with a shift in the start and end of the range by 1 for each additional day.
Another approach getting to the same result:
=LET(range,E2:AI2,
cons,4,
repeat,COLUMNS(range)-cons+1,
MAX(
BYROW(SEQUENCE(repeat,cons,,1)-INT(SEQUENCE(repeat,cons,0,1/cons))*(cons-1),
LAMBDA(x,SUM(INDEX(range,1,x))))))
This avoids OFFSET (volatile, slowing your file down) and the repeat value, consecutive number and/or the range are easily changeable.
Hope it helps (I answered to the max sum, as stated in the title). Change max to min to get the min sum result.
Edit:
I changed the repeat part in the formula to be dynamic (max number of consecutive columns in range), but you can replace it by a number or a cell reference.
The cons part can also be linked to a cell reference.
Also found a big in my formula which is fixed.

If function with formula to calculate difference between dates

I am trying to find a difference between 2 dates using networkdays and additionally if both dates are equal, have to make the value as 0. But unable to get the result.
You can try
if(DATEVALUE(A1)=DATEVALUE(A2),0,NETWORKDAYS(A1+1,B1)
#Ron Rosenfeld has pointed out that my testing led to wrong conclusions. Therefore I increased the volume of my tests. They continue to show that ...
Comparing the two dates and adding a day gives erratic results, and that
The naked NETWORKDAYS function as designed by Microsoft gives the most useful result.
In the table below the original function is in column C, your function (modified to work by #Ron Rosenfeld) in column D and two variations thereof in columns E and F. The lower part of the table shows results where both days are the same while the dates are 2 days apart in the upper part.
The tables show that the original formula counts both the start and the end days as well as all days between them, subtracting weekend days, except when start and end days are the same. In other words, the original function already treats the case of A1=B1 differently.
However, if an adjustment is to be made it should preferably be made to the result of the formula, not be modifying the date before they are processed. This is demonstrated in column F.
The formula in column E takes up your attempt to modify one of the dates (the end date could be modified too) and makes this conditional. The difference as compared to your formula is that you apply in fact two separate calculations, one where the result is always 0 (when dates are equal) and the other where the start date is advanced by one day unconditionally. If it suits your purposes that method can still be applied but the formula in column E shows how the date modification can be made conditional.
The need to apply conditions arises from the essential shortcoming of your formula in that it applies the wrong condition. Different treatment for when dates are equal is already incorporated in the basic function. Any further modification must take the involved weekdays into account, not (necessarily) only the interval between the dates.
Why do you put your formula arguments in quotes? That just changes them into strings, which is why your formula doesn't work.
I assume from your formula that you just don't want to count the first day. In which case it will work as you have it, if you merely remove the double quote marks.

Look for a value a cell above

I'm looking for a formula or a VBA code for the following problem.
I need to get the total operational hours in a day (J). This is de difference from the start-up time (I) and the shutdown time (K) over the whole day.
Most of the entries are 2 a day, but as you can see in the picture this one has 4. Previously I had the formula in J : =IF(K49="";0;IF(EXACT(A49;A48);(K49-I48);(K49-I49))) But with 4 entries this does not work anymore (the A49 and A28 are just numbers if they are the same it means that it is the same day)
So where I'm basically looking for is that it will take the shut downtime and looks for the first value in I and that it is calculating the difference in J.
Referring to another cell can be done, using OffSet(Row, Col). In case you need the cell just above, you use OffSet(-1,0).
I would just use some nested IFs to check first the row above, then the one above that, then the one more above.
Something like this (sorry, I cannot embed images):
https://i.stack.imgur.com/XLHVO.png

Sum the number of days in one date range which fall within a second date range

I have two columns of dates. One is the move in date and the other the move out date. I want to figure out how many days the tenant was there during a second date range. for example: how many total "bed days" did we have in the month of July? 7/1/2016-7/31/2016
This function calculates the number of days each tenant was there each month but I would like it if I could get the entire calculation into one cell without creating a dummy column for each month.
=MAX(0,MIN(EOMONTH($B$2,0),I14)-MAX($B$2,H14))
I tried to change a few things and use it as an array function but it is not working. I am very new to array functions so I may be doing it completely wrong.
=SUM(MAX(0,MIN(EOMONTH($B$2,0),I:I)-MAX($B$2,H:H)))
any help is much appreciated! Let me know if you need more info too.
Bad news - you can't use MAX and MIN in an array formula like this because instead of treating H and I as two arrays it just treats them as one big long array and you only get one value out of them.
You also need to add 1 to your original formula because if they moved in on the last day of the month (say) it should still count as one day.
If you replace the MAX and MIN with IF statements you get something like this
=SUM(IF(IF(EOMONTH($B$2,0)<IF(I2:I10="Active",TODAY(),I2:I10),EOMONTH($B$2,0),IF(I2:I10="Active",TODAY(),I2:I10))-IF($B$2>H2:H10,$B$2,H2:H10)<0,0,
IF(EOMONTH($B$2,0)<IF(I2:I10="Active",TODAY(),I2:I10),EOMONTH($B$2,0),IF(I2:I10="Active",TODAY(),I2:I10))-IF($B$2>H2:H10,$B$2,H2:H10)+1))
which has to be entered using CtrlShiftEnter
A useful tip if you are new to arrays is not to include more rows in an array formula than you need to because it will be slow, and test it first on a small number of rows so that you can step through it using Evaluate Formula if you run into trouble.

Excel: Counting how many rows fall within a time period

I have an excel sheet with 2 columns
Column A = "CA", "CR" or "IN"
Column B = Date & time format DD/MM/YYYY HH:MM
I want to make a count at the bottom of a column for each row that has this criteria:
i) Row 1-8 = "CA"
ii) Row 1-8 needs to check for a time range, namely > "17:00" and < "04:59"
This is what I've come up with so far:
=COUNTIFS(A2:A8,"CA",B2:B8,RIGHT(TEXT(B2:B8,"hh:mm"),5)>"04:59"), B2:B8,RIGHT(TEXT(B2:B8,"hh:mm"),5)<"17:00")
I presume using a range within a the text function is wrong, but don't know how to resolve this.
Because Column B is in a date and time format, I'm having to change it to a string within the function so I can make a test on just the time - Maybe there's a better way?)
Thanks
The problem with COUNTIFS is that you can't manipulate the conditions a lot. You could insert a column containing the time only, in which case you could use COUNTIFS but if you cannot, you can use SUMPRODUCT for substitute:
=SUMPRODUCT((A1:A6="CA")*(((TEXT(B1:B6,"hh:mm")*1>TIME(17,0,0))+(TEXT(B1:B6,"hh:mm")*1<TIME(5,0,0)))>0))
This applies a few conditions on an example range A1:B6:
(A1:A6="CA") that ensures that the row has CA,
(TEXT(B1:B6,"hh:mm")*1>TIME(17,0,0)) that ensures that the time is above 17:00
(TEXT(B1:B6,"hh:mm")*1<TIME(5,0,0)) that ensures that the time is before 05:00 (You have 4:59 in your question, if you really meant less than 4:59 then change this part).
The + for the last two conditions 'OR's the two conditions, then the whole thing is 'AND'ed with the first condition.
This is difficult to do with COUNTIFS because you can't modify ranges with functions.....but you can do that with SUMPRODUCT - try this
=SUMPRODUCT((A2:A8="CA")*(MOD(B2:B8,1)<"17:00"+0)*(MOD(B2:B8,1)>"04:59"+0))
I'm assuming that you want between 04:59 and 17:00 - in your point ii) you show it the opposite way to what you have in your formula
MOD extracts the time from the date/time so it can be compared against a time range. If you are counting within a range including whole hours, e.g. 5 to 16 inclusive you can use HOUR function without MOD, i.e.
=SUMPRODUCT((A2:A8="CA")*(HOUR(B2:B8)>=5)*(HOUR(B2:B8)<=16))
You do not need to deal with strings at all for the time. Excel provides with quite a few date&time functions, enough in your case.
A concise formula is
=SUMPRODUCT((A1:A8="CA")*(MOD(B1:B8,1)<=TIME(17,0,0))*(MOD(B1:B8,1)>TIME(4,59,0)))
Notes:
I assume you meant <=17:00, instead of <17:00. It is easy to modify the formula if I was wrong.
You used A2:A8, but you probably wanted A1:A8, as per the text. It is easy to modify the formula if I was wrong.
COUNTIFS is usually much less flexible than SUMPRODUCT (combined with other functions as MATCH, INDEX, SUM, etc.)
PS: as barry houdini points out, the OP asks for two opposite time ranges. I have chosen here one of them.

Resources