嵌入式" IF"公式偶尔会破坏,VBA替代?

时间:2018-04-10 16:31:42

标签: excel vba excel-vba if-statement

我有一个非常大的嵌入式IF公式,似乎偶尔会无缘无故地破坏。打开和关闭页面几次最终会让它再次运行。我想知道是否有VBA替代品。这是我正在运行的IF公式。

  

= IF(ISNUMBER(SEARCH(" 76210",E125))," _012_00762_10",IF(ISNUMBER(SEARCH(" 76220",E125)) " _012_00762_20",IF(ISNUMBER(SEARCH(" 76900",E125))," _012_00769_00",IF(ISNUMBER(SEARCH(" 76901&# 34;,E125))," _012_00769_01",IF(ISNUMBER(SEARCH(" 85702",E125))," _012_00857_02",IF(ISNUMBER(SEARCH( " 85710",E125))," _012_00857_10",IF(ISNUMBER(SEARCH(" 100800",E125))," _012_01008_00&#34 ;, IF(ISNUMBER(SEARCH(" 100900",E125))," _012_01009_00",IF(ISNUMBER(SEARCH(" 123100",E125)),&#34 ; _012_01231_00",IF(ISNUMBER(SEARCH(" 124600",E125))," _012_01246_00",IF(ISNUMBER(SEARCH(" 124601",E125 )),#34; _012_01246_01",IF(ISNUMBER(SEARCH(" 124640",E125))," _012_01246_40",IF(ISNUMBER(SEARCH(" 124641",E125))," _012_01246_41",IF(ISNUMBER(SEARCH(" 142301",E125))," _012_01423_01",IF(ISNUMBER( SEARCH(" 158801",E125))," _012_01588_01&#3 4;,IF(ISNUMBER(SEARCH(" 158900",E125))," _012_01589_00",IF(ISNUMBER(SEARCH(" 159203",E125)), #&34; _012_01592_03",IF(ISNUMBER(SEARCH(" 159303",E125))," _012_01593_03",IF(ISNUMBER(SEARCH(" 159401&#34 ;,E125))," _012_01594_01",IF(ISNUMBER(SEARCH(" 159410",E125))," _012_01594_10",IF(ISNUMBER(SEARCH(& #34; 159420",E125))," _012_01594_20",IF(ISNUMBER(SEARCH(" 159501",E125))," _012_01595_01",IF (ISNUMBER(SEARCH(" 169000",E125))," _012_01690_00",IF(ISNUMBER(SEARCH(" 186900",E125))," _012_01869_00",IF(ISNUMBER(SEARCH(" 213200",E125))," _012_02132_00",IF(ISNUMBER(SEARCH(" 213300",E125) )" _012_02133_00",IF(ISNUMBER(SEARCH(" 215400",E125))," _012_02154_00",IF(ISNUMBER(SEARCH(" 220100& #34;,E125))," _012_02201_00",IF(ISNUMBER(SEARCH(" 223800",E125))," _012_02238_00",IF(ISNUMBER(SEARCH (" 225600",E125))," _012_02256_ 00",IF(ISNUMBER(SEARCH(" 230700",E125))," _012_02307_00",IF(ISNUMBER(SEARCH(" 230701",E125) )" _012_02307_01",IF(ISNUMBER(SEARCH(" 231800",E125))," _012_02318_00",IF(ISNUMBER(SEARCH(" 235000& #34;,E125))," _012_02350_00",IF(ISNUMBER(SEARCH(" 235020",E125))," _012_02350_20",IF(ISNUMBER(SEARCH (" 242000",E125))," _012_02420_00",IF(ISNUMBER(SEARCH(" 246400",E125))," _012_02464_00" ,IF(ISNUMBER(SEARCH(" 292900",E125))," _012_02929_00"""))))))))))))) )))))))))))))))))))))))))

基本上它是构建的,因此扫描序列号,并为使用此工作表的用户填充单元格以及搜索结果。我已经在这张表中运行了一个宏。这就是......

Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim rng As Range

    Set rng = Intersect(Range("A2:A500, J2:J500"), Target) ' define range of interest
    If Not rng Is Nothing Then ' check it's not "nothing"
        If WorksheetFunction.CountA(rng) = rng.Count Then 'check for all of its cells being not empty
            On Error GoTo safe_exit 'add error control
            Application.EnableEvents = False 'don't do anything until you know something has to be done
            rng.Offset(, 1).Value = Date 'write Date next to all relevant changed cells
        End If
    End If

safe_exit:
    Application.EnableEvents = True
End Sub

也许有一种更好的方法可以使用不使用嵌入式IF语句的公式来构建此搜索,但我无法想到另一种方法。提前致谢。

2 个答案:

答案 0 :(得分:1)

任何时候你必须在y上超过2深,你可能想重新考虑使用。

您可以做的是根据您的值构建一个表。然后将该表作为查找的一部分引用。假设您的值列表在IF范围内,您可以使用公式D8:E45

表格的开头看起来像下面的内容。输入结果单元格引用您的输入值并拉出第二列的匹配。

Example Table with formula

要获取表格,您可以使用源公式并替换(查找和替换 - Ctrl + H )一些具有唯一分隔符的字符。然后使用文本到列 Alt + D + E 并分隔和复制>粘贴特殊>转置以快速使其接近您需要的格式

答案 1 :(得分:1)

这可能是您正在寻找的内容:

{{1}}

以数组公式(CTRL-SHIFT-ENTER)输入。

这里$ A $ 5:$ A $ 42包含76210,76220,...,292900(以文字形式输入,不是数字);和$ B $ 5:$ B $ 42包含_012_00762_10,_012_00762_20,...,_012_02929_00。

希望有所帮助。