简体   繁体   English

ArrayFormula超过255个字符。替换无效

[英]ArrayFormula More than 255 Characters .Replace Not Working

I have a large worksheet with a bunch of array formulas. 我有一个很大的工作表,上面有一堆数组公式。 I want to update these formulas using VBA to include an if statement. 我想使用VBA更新这些公式以包含if语句。 Basically the I want the new formula to be If(year>max_year, If_True_Formula, If_False_Formula). 基本上,我希望新公式为If(year> max_year,If_True_Formula,If_False_Formula)。

This is a fairly simple task to do by joining three strings and setting the ActiveCell.ArrayFormula = The result of the three strings. 通过联接三个字符串并设置ActiveCell.ArrayFormula =三个字符串的结果,这是一个相当简单的任务。 However, things start to get complicated if the resultant string exceeds 255 characters. 但是,如果结果字符串超过255个字符,事情就会变得复杂起来。 In several instances, the formula I needed did exceed 255 characters, so I added the placeholders "X_X" for the If_True_Formula and "Y_Y" for the If_False_Formula and used this method to create the complete formula: 在某些情况下,我需要的公式确实超过了255个字符,因此我为If_True_Formula添加了占位符“ X_X”,为If_False_Formula添加了占位符“ Y_Y”,并使用此方法创建了完整的公式:

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

This worked perfectly, but several of my array formulas are so long that the If_True portion and If_False portion were each more than 255 characters. 这很好用,但是我的几个数组公式太长了,以至于If_True部分和If_False部分都超过了255个字符。 To overcome this, I figured I could just divide each of these strings into three pieces (I won't a string more than 750 characters), and then using modified method to create the full string at the end: 为了克服这个问题,我认为我可以将这些字符串分成三个部分(我的字符串不会超过750个字符),然后使用修改后的方法在最后创建完整的字符串:

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

This method splits up my big string into three sub-strings, I could then use three replace statements to add to the existing string in three pieces. 这种方法将我的大字符串分成三个子字符串,然后我可以使用三个replace语句以三段形式添加到现有字符串中。

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

For some strange reason, the code runs without error but does not make any replacements for True_1, True_2, True_3. 由于某些奇怪的原因,该代码可以正确运行,但不会替换True_1,True_2,True_3。 If the initial string is not broken up, the code makes the appropriate replacement. 如果初始字符串未分解,则代码将进行适当的替换。 Very strange... 很奇怪...

I posted an answer here: Array formula with more than 255 characters 我在这里发布了答案: 超过255个字符的数组公式

The reason is, that the formulas have to make sense to be replaced at any time. 原因是必须随时替换公式。 So you can't replace with Strings. 因此,您不能用字符串代替。 I made a function that works for english function in A1-notation. 我制作了一个适用于A1表示法英语功能的函数。 If you use the if-command in the formula several times and never used the line 1337 you can copy it and use it without any efford. 如果您多次在公式中使用if-command,但从未使用过1337行,则可以复制它,并在没有任何efford的情况下使用它。

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

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