Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

Exclude auto count record based on specific condition

0

Hi guys.

I want to count total records  based on coumn A

A1 : 09:00-14:00

A2 : 1500:16:00

A3 : no input

A4: 10:00-11:00

A5:14:00-18:00

B1 : check coumn A end time is less or =16:00 will count 1 , this example will show 3 in b1.

I try use this formula but not success.

=SUMPRODUCT(--(TIMEVALUE(MID(A1:A6, FIND("-", A1:A6) + 1, LEN(A1:A6) - FIND("-", A1:A6))) <= TIME(16, 0, 0)))

thanks in advanced.

Answer
Discuss

Answers

0
Selected Answer

Hi Fai and welcome to the Forum.

In the attached file I've created two working array formulas...

In cell B1 (yellow) you will see:

=SUMPRODUCT(IFERROR(IF(TIMEVALUE(RIGHT(TRIM(A:A),5))<=TIME(16,0,0),1,0),0), IFERROR(IF(FIND("-",A:A)>0,1,0),0))

which works on all of column A (as per your question working) and detects "-" in the cells and if the last 5 characters indicate a time <16:00.

B6 (green) is similar but works on just A1:A5-

=SUMPRODUCT(IFERROR(IF(TIMEVALUE(RIGHT(TRIM(A1:A5),5))<=TIME(16,0,0),1,0),0), IFERROR(IF(FIND("-",A1:A5)>0,1,0),0))

Hope this fixes your problem. If so, please be sure to mark this Answer as Selected.

Revision #1, 22 September 2024

Please note that this Answer relies on array formulas. In Excel 365, and 2021, they can be entered (or edited) and confirmed with the Enter key. In legacy versions (e.g. 2019 and earlier) you need to confirm the formula using the key combination Ctrl+Shift+Enter or they will produce the wrong result. 

Discuss

Discussion

Hi John thank for help
By using your provied formula and try to copy next cell to test, but still have problem. 

=SUMPRODUCT(IFERROR(IF(TIMEVALUE(RIGHT(TRIM(A:A),5))<=TIME(16,0,0),1,0),0), IFERROR(IF(FIND("-",A:A)>0,1,0),0)) Copy in b2 answer is 1

=SUMPRODUCT(IFERROR(IF(TIMEVALUE(RIGHT(TRIM(A1:A5),5))<=TIME(16,0,0),1,0),0), IFERROR(IF(FIND("-",A1:A5)>0,1,0),0)) copy to cell b7 answer is 0

I am using excel 2016 version. 




fai (rep: 2) Sep 20, '24 at 9:20 pm
Fai. 

My Answer was a bit rushed and I didn't say it relies on array formulas. These can be entered (or edited) in Excel 365 and confirmed with tge E ter key. In legacy versions (e.g. 2016) you need to confirm the formula using tge key Ctrl+Shift+Enter

Try that and respond please 
John_Ru (rep: 6417) Sep 21, '24 at 1:30 am
Hi again Fia

I got home and tried my file with Excel 2010. You said "Copy in b2 answer is 1" but if I copy B1 to B2 in Excel 2010, it gives the correct answer (3). If however I look at the equation in the formula bar and press Enter, it gives the wrong answer (1). If I do the same again and confirm it with Ctrl+Shift+Enter instead then the array formula works and shows the correct answer (3).

Please try this and (hopefully) mark my Answer as Selected. If I get time on Sunday, I will revise my Answer to give that explanatory note (for users of legacy versions of Excel).
John_Ru (rep: 6417) Sep 21, '24 at 8:50 am
Thanks for selecting my Answer, Fai. I added a note to my Answer- see Revision #1 near the end. 
John_Ru (rep: 6417) Sep 22, '24 at 2:27 am
Add to Discussion
0

Hi John and thanks to help again.

=SUMPRODUCT(IFERROR(IF(TIMEVALUE(RIGHT(TRIM(A:A),5))<=TIME(16,0,0),1,0),0), IFERROR(IF(FIND("-",A:A)>0,1,0),0)) or =SUMPRODUCT(IFERROR(IF(TIMEVALUE(RIGHT(TRIM(A1:A5),5))<=TIME(16,0,0),1,0),0), IFERROR(IF(FIND("-",A1:A5)>0,1,0),0)) in the formula bar confirm it with Ctrl+Shift+Enter will work perfect. 

Discuss

Discussion

Hi again Fai. Thanks for your comment above should it be under the Discussion to my Answer, not as an Answer - these are meant to be solutions to user Questions. If possible please delete the Answer above. 
John_Ru (rep: 6417) Sep 22, '24 at 2:31 am
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login