Excel函数限制8192个字符

时间:2017-02-07 16:24:15

标签: excel vba excel-vba excel-formula

我在一个单元格中有超过10,000个字符的公式。我不能引用多个单元格,因为要求限制它。

示例:

=IF(NOT(ISERROR(SEARCH(String1,IFERROR(IF(INDEX(INDIRECT(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE($J2,"&","_"),"-","_")," ",""),":","_")),COLUMNS($K$1:K$1))="","",INDEX(INDIRECT(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE($J2,"&","_"),"-","_")," ",""),":","_")),COLUMNS($K$1:K$1))),"")))),SUBSTITUTE(IFERROR(IF(INDEX(INDIRECT(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE($J2,"&","_"),"-","_")," ",""),":","_")),COLUMNS($K$1:K$1))="","",INDEX(INDIRECT(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE($J2,"&","_"),"-","_")," ",""),":","_")),COLUMNS($K$1:K$1))),""),String1,$G2),IF(NOT(ISERROR(SEARCH(String2,IFERROR(IF(INDEX(INDIRECT(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE($J2,"&","_"),"-","_")," ",""),":","_")),COLUMNS($K$1:K$1))="","",INDEX(INDIRECT(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE($J2,"&","_"),"-","_")," ",""),":","_")),COLUMNS($K$1:K$1))),"")))),SUBSTITUTE(IFERROR(IF(INDEX(INDIRECT(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE($J2,"&","_"),"-","_")," ",""),":","_")),COLUMNS($K$1:K$1))="","",INDEX(INDIRECT(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE($J2,"&","_"),"-","_")," ",""),":","_")),COLUMNS($K$1:K$1))),""),String2,$F2), IFERROR(IF(INDEX(INDIRECT(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE($J2,"&","_"),"-","_")," ",""),":","_")),COLUMNS($K$1:K$1))="","",INDEX(INDIRECT(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE($J2,"&","_"),"-","_")," ",""),":","_")),COLUMNS($K$1:K$1))),"")))

我不确定该公式是否有帮助。但那个公式的那个例子。

在Excel中获得了命名范围(定义的名称),公式可以根据该范围的名称调用单元格。

是否有类似公式/功能的东西?因为我的函数很长,因为嵌套公式。

我可以将“SUBSTITUTE”替换为我自己的角色吗?像前。的 SUBTE

3 个答案:

答案 0 :(得分:3)

VBA可能是最好的选择。 不过,如果你不能考虑它,可以考虑将公式分成5到6个较小的公式。它可能会更好,使用该公式的人有更多机会了解正在发生的事情:

E.g。像这样: enter image description here 这里第二行的公式在第一行被分成另外两个公式。

注意:Wenn表示IF

此外 - 当你使用如此大的公式时,总是尝试以有意义的方式格式化它们。例如,像这样:

enter image description here

通过点击您想要新行的地方,按ALT+Enter即可实现此目的。代表所有人,每天处理写得不好的公式 - 谢谢! :)

编辑: 无论如何,如果VBA是一个选项,最糟糕的情况就是这样:

Option Explicit

Public Function MyLongFormula() As String

    Dim strResult As String

    Application.Volatile

    strResult = "=IF(NOT(ISERROR(SEARCH(String1,IFERROR(IF(INDEX(INDIRECT(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE($J2,""&"",""_""),""-"",""_""),"" "",""""),"":"",""_"")),"
    strResult = strResult & "COLUMNS($K$1:K$1))="""","""",INDEX(INDIRECT(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE($J2,""&"",""_""),""-"",""_""),"" "",""""),"":"",""_"")),"
    strResult = strResult & "COLUMNS($K$1:K$1))),"""")))),SUBSTITUTE(IFERROR(IF(INDEX(INDIRECT(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE($J2,""&"",""_""),""-"",""_""),"" "",""""),"":"",""_"")),"
    strResult = strResult & "COLUMNS($K$1:K$1))="""","""",INDEX(INDIRECT(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE($J2,""&"",""_""),""-"",""_""),"" "",""""),"":"",""_"")),COLUMNS($K$1:K$1))),""""),"
    strResult = strResult & "String1,$G2),IF(NOT(ISERROR(SEARCH(String2,IFERROR(IF(INDEX(INDIRECT(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE($J2,""&"",""_""),""-"",""_""),"" "",""""),"":"",""_"")),"
    strResult = strResult & "COLUMNS($K$1:K$1))="""","""",INDEX(INDIRECT(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE($J2,""&"",""_""),""-"",""_""),"" "",""""),"":"",""_"")),COLUMNS($K$1:K$1))),"""")))),"
    strResult = strResult & "SUBSTITUTE(IFERROR(IF(INDEX(INDIRECT(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE($J2,""&"",""_""),""-"",""_""),"" "",""""),"":"",""_"")),COLUMNS($K$1:K$1))="""","""","
    strResult = strResult & "INDEX(INDIRECT(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE($J2,""&"",""_""),""-"",""_""),"" "",""""),"":"",""_"")),COLUMNS($K$1:K$1))),""""),String2,$F2),"
    strResult = strResult & "IFERROR(IF(INDEX(INDIRECT(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE($J2,""&"",""_""),""-"",""_""),"" "",""""),"":"",""_"")),COLUMNS($K$1:K$1))="""","""","
    strResult = strResult & "INDEX(INDIRECT(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE($J2,""&"",""_""),""-"",""_""),"" "",""""),"":"",""_"")),COLUMNS($K$1:K$1))),"""")))"

    MyLongFormula = Evaluate(strResult)

End Function

Public Function MyLongFormula2() As String

    Application.Volatile
    MyLongFormula2 = Evaluate("=sum(1,2)")

End Function

只需写下=MyLongFormula2即可了解预期会如何发挥作用。

答案 1 :(得分:3)

如前所述,最好的解决方案几乎肯定是编写一个整洁的VBA功能,它可以为您完成所有工作。

然而,正如你明确要求的那样。以下是只是SUBSTITITE 但长度较短的函数的代码。我无法看到您使用instance_num参数的任何地方,因此我没有将其构建。

您需要在VBA编辑器中将以下代码添加到新模块中,然后只需编写SUBSTITUTE()

即可使用工作表上的SU()功能
Function SU(txt As String, old_text As String, new_text As String) As String
    SU = Application.WorksheetFunction.Substitute(txt, old_text, new_text)
End Function

答案 2 :(得分:2)

不幸的是,你的问题的答案是“不”,或者至少“不完全”。您不能直接命名函数,但可以使用named formulas。例如,您不能使用名称为函数S创建别名SUM(),但是您可以创建一个命名公式S,当它出现在另一个内部时公式将对单元格上方的100个元素求和。通过一定程度的聪明,您通常可以使用命名公式来缩短非常长的公式,但不能像引入函数缩写那样机械化。

可以使用VBA来创建UDF函数,这些函数是工作表函数的缩写别名,但是如果要使用VBA,通常可以用函数等效的UDF替换整个​​公式,而不是简单地替换别名的一些功能。请参阅@CallumDA的答案,了解工作表函数的VBA别名的含义。