扩展数字字符串公式

时间:2016-11-19 20:07:45

标签: excel excel-formula

我有一个Excel公式,它搜索一个包含4个数字的字符串,在单个单元格中用逗号分隔,然后将它们除以2(向下舍入)。

e.g。单元格A1 = 5,35,44,7
单元格中的公式B1 = 2,17,22,3

我想扩展公式以适应从1到15的任意数字的字符串。当前公式适用于1到4个数字的字符串,如下所示。我想在不使用VBA的情况下将公式保留在单个单元格中。关于如何扩展我现有的想法?

=LEFT(IFERROR(ROUNDDOWN(MID(A2,1,IFERROR(SEARCH(",",A2,1)-1,LEN(A2)))/2,0),"")&","&IFERROR(ROUNDDOWN(MID(A2,SEARCH(",",A2,1)+1,IFERROR(SEARCH(",",A2,SEARCH(",",A2,1)+1)-SEARCH(",",A2,1)-1,LEN(A2)-SEARCH(",",A2,1)))/2,0),"")&","&IFERROR(ROUNDDOWN(MID(A2,SEARCH(",",A2,SEARCH(",",A2,1)+1)+1,IFERROR(SEARCH(",",A2,SEARCH(",",A2,SEARCH(",",A2,1)+1)+1)-SEARCH(",",A2,SEARCH(",",A2,1)+1)-1,LEN(A2)-SEARCH(",",A2,SEARCH(",",A2,1))))/2,0),"")&","&IFERROR(ROUNDDOWN(MID(A2,SEARCH(",",A2,SEARCH(",",A2,SEARCH(",",A2,1)+1)+1)+1,IFERROR(SEARCH(",",A2,SEARCH(",",A2,SEARCH(",",A2,SEARCH(",",A2,1)+1)+1)+1)-SEARCH(",",A2,SEARCH(",",A2,SEARCH(",",A2,1)+1)+1)-1,LEN(A2)-SEARCH(",",A2,SEARCH(",",A2,SEARCH(",",A2,1)+1)+1)))/2,0),""),LEN(A2))

1 个答案:

答案 0 :(得分:3)

这种方法可以处理单个单元格中几乎无限数量的项目,但它需要多个公式。

将值放在 A1 中。在 A2 中输入:

=IFERROR(--ROUNDDOWN(TRIM(MID(SUBSTITUTE(A$1,",",REPT(" ",999)),ROWS($1:1)*999-998,999))/2,0),"")

并复制下来。 (这些是已解析的单个值,除以2,然后向下舍入)

B2 中输入:=A2。在 B3 中输入:

=B2 & IF(A3="","","," & A3)

并复制下来。 (执行重新连接)。最后在 C1 中输入:

=INDEX(B:B,MATCH("",A:A,0))

enter image description here

使用 VBA 来解决这个问题要容易得多。这个小的 UDF()做同样的事情:

Public Function Brad(inpt As String) As String
    Dim a
    ary = Split(inpt, ",")
    With Application.WorksheetFunction
        For Each a In ary
            Brad = Brad & "," & .RoundDown(CDbl(a) / 2, 0)
        Next a
        Brad = Mid(Brad, 2)
    End With
End Function