开发者

complex EXCEL IF formula or VBA

开发者 https://www.devze.com 2023-01-06 16:34 出处:网络
i have this data set: 92127 96001-1 94533-1 95630 95677 95630 i need this output 92127-1 95630-1 95677-1 95630-2

i have this data set:

92127
96001-1
94533-1
95630
95677
95630

i need this output

92127-1
95630-1
95677-1
95630-2

he开发者_StackOverflowre is the logic:

if 92127 does not exist in this column, then it should be 92127-1; however, if it DOES exist, then it should be 92127-(what ever the largest number is here +1), so if the largest one is 92127-5, then it should make it 92127-6

is it possible to make this into a formula?


Here's mine:

=IF(LEN(A2)>5,A2,A2&"-"&SUMPRODUCT(--(LEFT($A$2:$A2,5)=LEFT(A2,5))))

A2:A7

92127
96001-1
94533-1
95630
95677
95630

B2:B7 (where you put the formula)

92127-1
96001-1
94533-1
95630-1
95677-1
95630-2

Edit: The above assumes that first instance of the five digit leading number will have no -n and uses the count of that five digit leading number to append the -n to subsequent instances. However, if the first instance of, say, 95630, is 95630-2 then the above fails. String manipulation does not work well with array formulas, so I think the only option is to use helper columns. With your data in A2:A7,

B2: =IF(ISERR(FIND("-",A2)),A2,LEFT(A2,5))
C2: =IF(ISERR(FIND("-",A2)),0,MID(A2,FIND("-",A2)+1,LEN(A2)))
D2: =B2&"-"&IF(ISERR(FIND("-",A2)),MAX(($B$2:$B$7=B2)*($C$2:$C$7))+1,C2)

and fill down to row 7. B2 and C2 split the number into the five leading digits and the -n suffix, if any. If there is no -n, it returns a 0. D2 is entered with Control+Shift+Enter because it's an array formula. It finds the maximum -n for the current five leading digits, adds one, and appends it to the five leading digits.

Helper columns make it more cumbersome, but I couldn't find a better way.


Since you want to keep the '-1' on whatever is there (as per comment), this should work:

Assuming 'Existing' is in cells A2:An, this formula goes in B2, and can be copied down to Bn.

=LEFT(A2,5)&TEXT(COUNTIF(A$2:A2,LEFT(A2,5))+IF(ISERROR(VALUE(MID(A2,7,1))),0,VALUE(MID(A2,7,1))),"-0;;")

My results are this:

Existing

92127
96001-1 
94533-1 
95630
95677
95630

New

92127-1
96001-1
94533-1
95630-1
95677-1
95630-2
0

精彩评论

暂无评论...
验证码 换一张
取 消