ArrayFormula超过255个字符。replace不工作

我有一大堆数组公式的大工作表。 我想用VBA来更新这些公式来包含一个if语句。 基本上我想要新的公式是如果(年> max_year,If_True_Formula,If_False_Formula)。

通过连接三个string并设置ActiveCell.ArrayFormula =三个string的结果,这是一个相当简单的任务。 但是,如果结果string超过了255个字符,情况开始变得复杂。 在一些情况下,我需要的公式超过了255个字符,所以我添加了If_True_Formula的占位符“X_X”和If_False_Formula的“Y_Y”,并使用这个方法创build了完整的公式:

With ActiveCell .FormulaArray = If_Statement .Replace "X_X", If_True .Replace "Y_Y", If_False End With 

这工作得很好,但是我的几个数组公式很长,If_True部分和If_False部分每个都超过了255个字符。 为了克服这个问题,我想我可以把每个string分成三部分(我不会超过750个字符的string),然后使用修改的方法在最后创build完整的string:

 True_1 = "" If len(If_True) > 255 Then True_1 = Left(If_True, len(If_True)/3) & "X2_X2" True_2 = Mid(If_True, len(If_True)/3, len(If_True)/3) & "X3_X3" True_3 = Mid(If_True, len(True_1) + len(True_2) - 10, len(If_True)) End If 

这种方法将我的大string分割成三个子string,然后我可以使用三个replace语句将三个string添加到现有string中。

 With ActiveCell .FormulaArray = If_Statement If True_1 = "" Then .Replace "X_X", If_True Else .Replace "X_X", True_1 .Replace "X2_X2", True_2 .Replace "X3_X3", True_3 End If End With 

出于某种奇怪的原因,代码运行时没有错误,但不会replace为True_1,True_2,True_3。 如果初始string没有被分解,则代码将进行适当的replace。 很奇怪…

我在这里发布了一个答案: 超过255个字符的数组公式

原因是,这些公式必须随时取代才有意义。 所以你不能用stringreplace。 我用A1表示法为英文函数做了一个函数。 如果在公式中多次使用if命令并且从未使用过1337行,则可以复制并使用它。