problem with rank and countif function

mychi11

Board Regular
Joined
May 11, 2020
Messages
95
Office Version
  1. 2016
Platform
  1. Windows
I tried to use the formula
=IF($E2<>"IUI",RANK(C2,$C:$C,1)+COUNTIF($C$2:C2,C2)-1) to generate sequential number based on date without repetition.
This formula works well. However, when i add in new row, the formula in the last few row tends to change. For example =IF($E68<>"IUI",RANK(C68,$C:$C,1)+COUNTIF($C$2:C69,C68)-1).
Is there anyway to improve the formula?
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Are you using a structured table, or regular ranges?

I've seen problems where relative rows adjust incorrectly when rows are added to a table, but not for a normal range.
 
Upvote 0
It is possible to correct the formula so that the rows adjust correctly. You will need to edit the formula below so that the names match your actual table.

=IF([@Col_E]<>"IUI",RANK([@Col_c],[Col_C],1)+COUNTIF(INDEX([Col_C],1):INDEX([Col_C],ROW([@Col_C])-ROW([[#Headers][Col_C]])),[@Col_C])-1)

Hopefully the parentheses are all in the right places.
 
Upvote 0
Hi Jason,

Thank you for your prompt response. After applying the formula, i realised no 1 and 2 are missing and I am not sure why N2 and N3 both rank 3.



ART RECORD SHEET(IVF(HK)).xlsm
CDEFGHIJKLMNOPQRST
1PROCEDURE DATEART DATEPROCEDURES IUI DATEOPU DATEOC WARM DATEET DATEFET DATEEMB WARM DATEART NO.IUI NOIVF NOCY NO (OC THW)CY NO. (FET)CY NO. (ALL)TRWIFE NAMEDOB(W)
214-05-2014-05-20ICSI14-05-2019-05-20200514-01321AMY01-05-87
315-05-2015-05-20ICSI15-05-20 200515-01311ACE27-08-86
418-05-2018-05-20IVF18-05-20 200514-02 431ALICE20-05-82
523-06-2023-06-20ICSI23-06-20 200515-01 542ACE27-08-86
607-07-2007-07-20ICSI07-07-2010-07-20200706-01 651GRACE01-11-80
702-08-2002-08-20ICSI02-08-2005-08-20200706-01 1362GRACE01-11-80
808-08-2008-08-20OC FZ08-08-20 200514-01 1971AMY01-05-87
908-08-2008-08-20OC FZ08-08-20 200707-01 2081LILY22-12-78
1017-08-2017-08-20BOTH17-08-2020-08-20200622-02 2291KELLY18-07-89
1118-08-2000-01-00FET 18-08-2018-08-20200514-01 232 1AMY01-05-87
1227-08-2000-01-00FET 27-08-2025-08-20200515-01 361 1ACE27-08-86
1324-08-2024-08-20OC THW+ICSI24-08-20 200514-01 277102AMY01-05-87
1410-09-2010-09-20OC FZ10-09-20 200707-01 45112LILY22-12-78
1515-09-2000-01-00FET 15-09-2015-09-20200514-02 473 1ALICE20-05-82
1602-09-2002-09-20OC THW+ICSI02-09-2005-09-20200514-01 407#####3AMY01-05-87
1723-11-2000-01-00FET 23-11-2023-11-20200514-01 542 2AMY01-05-87
1812-12-2000-01-00FET 12-12-2012-12-20200706-01 566 1GRACE01-11-80
1912-12-2000-01-00FET 12-12-2012-12-20200622-02 579 1KELLY18-07-89
ART
 
Upvote 0
Looks like you lost an important little bit of the formula :eek:

=IF([@[PROCEDURES ]]<>"IUI",RANK([@[PROCEDURE DATE]],[PROCEDURE DATE],1)+COUNTIF(INDEX([PROCEDURE DATE],1):INDEX([PROCEDURE DATE],ROW([@[PROCEDURE DATE]])-ROW([[#Headers][PROCEDURE DATE]])),[@[PROCEDURE DATE]])-1)
 
Upvote 0
it's because when i add in the header part, an message : the syntax of the name isnt correct. :( not sure what exactly is wrong
 
Upvote 0
Hi Jason, I have solved the problem by adding an additional coma. Thank you so much for your help. It's working perfectly
 
Last edited:
Upvote 0
I just found the problem but look like you beat me to it. Table formulas are always a pain to write.

=IF([@[PROCEDURES ]]<>"IUI",RANK([@[PROCEDURE DATE]],[PROCEDURE DATE],1)+COUNTIF(INDEX([PROCEDURE DATE],1):INDEX([PROCEDURE DATE],ROW([@[PROCEDURE DATE]])-ROW([[#Headers],[PROCEDURE DATE]])),[@[PROCEDURE DATE]])-1)
 
Upvote 0
Thank you so much for your help. Sorry I don't have much knowledge in writing these formulas. I basically googled everything to make up the formula, so it's difficult for me to spot what is missing or so. Thank you once again, Jason.
 
Upvote 0

Forum statistics

Threads
1,215,376
Messages
6,124,593
Members
449,174
Latest member
chandan4057

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top