简体   繁体   中英

Excel formula to calculate the time a device is on and off

I have a dehumidifier that turns on and off. I want to find out how many minutes it is on and then how many minutes it is off. I have three cells:

A1 - Date 
B1 - Time 
C1 - Status (On or Off)

I need a formula that will look in Cell C1 and if it contains "On" then continue looking at all consecutive cells for the last "On" and then calculate the time difference in column B and place this calculated value in column D. Below I show the calculated minutes in the last row where the status is found, but it doesn't matter if it is the first or last instance of the value "On". I've got the hard part of calculating the difference in time down. I need help with the easy part of searching through the consecutive on's and off's and calculating the difference between the times. I've tried using vlookup, index and match but come up empty each time. Below is an image of the table.

样本数据集

Thanks in advance for any assistance.

Enter the following formula in Cell D2:

=IF(C2=C3,"",ROW(C2)-LARGE(IF($D$1:D1<>"",ROW($D$1:D1)),1)-1)

Use the fill handle to drag down.

Note: This formula assumes that each row represents one minute, and counts accordingly. This will not work if a row skips a minute.

That produces an integer with how many minutes elapsed. If you want 0:05:00 format, use this formula:

=IF(C2=C3,"",(ROW(C2)-LARGE(IF($D$1:D1<>"",ROW($D$1:D1)),1)-1)*0.000694444444444444)

Format Column D using this format:

[h]:mm:ss

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM