最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

excel - What are some possible solutions to identify the next non-existing value? - Stack Overflow

programmeradmin2浏览0评论

I'm currently working on a scenario where I need to find the next available ID within a list of unique IDs that contain both letters and numbers.

While I have a working solution, I'm interested in exploring whether there might be a more efficient method, ideally using a single formula if possible.

A: unique IDs
B: do not exist(just shown for demostration)
C: A+1 =LEFT(A1,1)&RIGHT(A1,4)+1
D: check if +1 ID exist in current list(A column)=COUNTIF($A$1:$A$9,C1)

In a different cell using index and match to find "0" from D and index back to column A.

`=INDEX($C$1:$D$9,MATCH(0,$D$1:$D$9,0),1)`

If search keyword is A1, the answer should be A1004, whereas keyword is B2 should result to B2002.

Thank you in advance.

A B C D
A1000 +1 A1001 1
A1001 +1 A1002 1
A1002 +1 A1003 1
A1003 +1 A1004 0
B1000 +1 B1001 1
B1001 +1 B1002 0
B2000 +1 B2001 1
B2001 +1 B2002 0

I'm currently working on a scenario where I need to find the next available ID within a list of unique IDs that contain both letters and numbers.

While I have a working solution, I'm interested in exploring whether there might be a more efficient method, ideally using a single formula if possible.

A: unique IDs
B: do not exist(just shown for demostration)
C: A+1 =LEFT(A1,1)&RIGHT(A1,4)+1
D: check if +1 ID exist in current list(A column)=COUNTIF($A$1:$A$9,C1)

In a different cell using index and match to find "0" from D and index back to column A.

`=INDEX($C$1:$D$9,MATCH(0,$D$1:$D$9,0),1)`

If search keyword is A1, the answer should be A1004, whereas keyword is B2 should result to B2002.

Thank you in advance.

A B C D
A1000 +1 A1001 1
A1001 +1 A1002 1
A1002 +1 A1003 1
A1003 +1 A1004 0
B1000 +1 B1001 1
B1001 +1 B1002 0
B2000 +1 B2001 1
B2001 +1 B2002 0
Share Improve this question edited Jan 30 at 7:19 Black cat 6,2995 gold badges28 silver badges60 bronze badges asked Jan 30 at 4:02 MakiMaki 6374 gold badges12 silver badges26 bronze badges 1
  • Question is confusing. Can you state which column is your desired result? – Harun24hr Commented Jan 30 at 4:38
Add a comment  | 

3 Answers 3

Reset to default 1

I think you can first replace all of the formulas you use with the array version.

e.g Step1, use =LEFT(A1:A8, 1) & RIGHT(A1:A8, 4) + 1 to get "next value"

Step2, use =COUNTIF(A1:A8, LEFT(A1:A8, 1) & RIGHT(A1:A8, 4) + 1) to check whether "next value" exist or not.

Then, to filter out next "unavailable value", you can use XLOOKUP, but you need to use XLOOKUP on ranges that exclude "previous rows".

e.g for A1000, the search starts from A1001, for A1002, the search starts from A1003, for B2000, the search starts from B2002.

so the single formula would be:

=LET(
    next_vals, LEFT(A1:A8, 1) & RIGHT(A1:A8, 4) + 1,
    cnts, COUNTIF(A1:A8, next_vals),
    MAP(SEQUENCE(ROWS(cnts)), LAMBDA(row_num, XLOOKUP(0, DROP(cnts, row_num - 1), DROP(next_vals, row_num - 1))))
)

Here is the results:

Also, noted, this solution is based on the assumption that values in column A is in ascending order.

This formula

=UPPER(B14)&TEXT(1+MAX(--MID(FILTER(A1:A8,LEFT(A1:A8,2)=B14),3,9999)),"000")

will give the next unique value.

Parameters:

A1:A8 the range of the existing unique IDs. Five characters long.
B14 is the cell of the searched group of IDs. Two characters long.

Edit, this will give you the next available ID for each prefix:

=LET(
    ids, $A$1:$A$9,
    prefix, LEFT(ids, 2),
    unique_prefixes, UNIQUE(prefix),
    result, MAP(unique_prefixes, LAMBDA(r, r & TEXT(MAX(IF(LEFT(ids, 2) = r, RIGHT(ids, 3) * 1))+1, "000"))),
    result
)

发布评论

评论列表(0)

  1. 暂无评论