简体   繁体   English

复杂的EXCEL IF公式或VBA

[英]complex EXCEL IF formula or VBA

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

here is the logic: 这是逻辑:

if 92127 does not exist in this column, then it should be 92127-1 ; 如果该列中不存在92127 ,则应为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 但是,如果确实存在,则应该为92127-(这里最大的数字为+1),因此,如果最大的是92127-5,则应该为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 A2:A7

92127
96001-1
94533-1
95630
95677
95630

B2:B7 (where you put the formula) B2:B7(放置公式的位置)

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. 编辑:上面假定五位数字开头的第一个实例将没有-n,并使用该五位数字开头的计数将-n附加到后续实例。 However, if the first instance of, say, 95630, is 95630-2 then the above fails. 但是,如果第一个实例(例如95630)是95630-2,则以上操作将失败。 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, 将数据保存在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. 并填充到第7行。B2和C2将数字分为五个前导数字和-n后缀(如果有)。 If there is no -n, it returns a 0. D2 is entered with Control+Shift+Enter because it's an array formula. 如果不存在-n,则返回0。因为D2是数组公式,所以使用Control + Shift + Enter输入D2。 It finds the maximum -n for the current five leading digits, adds one, and appends it to the five leading digits. 它查找当前五个前导位数的最大值-n,将其加一,并将其​​附加到五个前导位数。

Helper columns make it more cumbersome, but I couldn't find a better way. Helper列使其变得更加繁琐,但是我找不到更好的方法。

Since you want to keep the '-1' on whatever is there (as per comment), this should work: 由于您想将“ -1”保持在存在的位置(根据注释),因此应该可以:

Assuming 'Existing' is in cells A2:An, this formula goes in B2, and can be copied down to Bn. 假设“现有”位于单元格A2:An中,则此公式将进入B2,并且可以向下复制到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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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