【问题标题】:Google Sheets / Excel - "if A, then B, if D, then E" matching谷歌表格/Excel - “如果A,那么B,如果D,那么E”匹配
【发布时间】:2017-02-27 18:23:34
【问题描述】:

在 Google 表格或 Excel 中,是否有一种简单的方法可以根据另一个单元格的几个可能值之一将值插入到一个单元格中?

换句话说,

  • 如果A1 包含“红色”,则B1 应填写“危险”
  • 如果A1 包含“蓝色”,则B1 应填写“正常”
  • 如果A1 包含“绿色”,则B1 应填写“继续”

写这种匹配的语法是什么?

提前致谢!

【问题讨论】:

  • 当您说“包含”时,您是指单元格中的任何位置,例如 This cell is Red 还是单元格的值,只有该单词在单元格中。
  • 这种方式可以嵌套很多 IF 语句,但是查找表可能会大大简化事情。

标签: excel google-sheets excel-formula


【解决方案1】:

B1中输入:

=LOOKUP(A1,{"blue","green","red"},{"normal","proceed","hazard"})

注意查找值必须按字母顺序排列。

【讨论】:

  • 在提供此类解决方案时,我认为谨慎地提及{"blue","green","red"} 必须按字母升序排列。
  • 因为是二分查找,连第一个字母都够了=LOOKUP(A1,{"b","g","r";"normal","proceed","hazard"})
【解决方案2】:

使用 B1 中的以下行来获得答案。确保 A1 中的拼写正确,否则如果 A1 为空或拼写错误,您将看到 FALSE。

=IF(A1="red","Hazard",IF(A1="blue","normal",IF(A1="green","proceed")))

【讨论】:

    【解决方案3】:

    这是基于单元格颜色的不同方法

    选项显式

    Public Function ColorIndexes(Rango As Range) As Variant
     Application.Volatile
     If Rango.Interior.ColorIndex = 3 Then
      ColorIndexes = "Hazzard"
       ElseIf Rango.Interior.ColorIndex = 24 Or Rango.Interior.ColorIndex = 37 Or Rango.Interior.ColorIndex = 23 Or Rango.Interior.ColorIndex = 49 Then
        ColorIndexes = "Normal"
         ElseIf Rango.Interior.ColorIndex = 50 Or Rango.Interior.ColorIndex = 35 Or Rango.Interior.ColorIndex = 48 Or Rango.Interior.ColorIndex = 12 Or Rango.Interior.ColorIndex = 56 Or Rango.Interior.ColorIndex = 14 Then
          ColorIndexes = "Proceed"
           Else
           ColorIndexes = "Not matched"
          End If
    
    End Function
    

    【讨论】:

      猜你喜欢
      • 2016-05-02
      • 1970-01-01
      • 2011-03-09
      • 1970-01-01
      • 1970-01-01
      • 2020-07-31
      • 2016-10-21
      • 2012-01-27
      • 2023-03-21
      相关资源
      最近更新 更多