【问题标题】:If range has specific comment, show msg in another cell如果范围有特定注释,则在另一个单元格中显示 msg
【发布时间】:2021-07-12 06:55:20
【问题描述】:

我在 VBA 中有这段代码,并要求它在另一张纸上显示“最终装瓶”。下面是代码

Ip = input worksheet
op1 = Checks worksheet
i = 1
Cell = Ip.Cells(9, i + 2)
    If LCase(Left(Cell, 14)) = "final bottling" Then
                                                                                                '#Checks Final Bottling
            Op1.Cells(8, 5) = "Final Bottling Run, Please Consume materials. If unsure, check with materials planner!"
        
        Else
        
            Op1.Cells(8, 5) = ""

    End If 'Check

如果 C9:H9 范围内的所有 cmets 都有注释“最终装瓶”,则会显示该消息。但是,如果该范围内只有一个单元格具有注释,它将不再出现。 不知道现在该怎么办,如果这听起来很愚蠢并且必须很容易解决,请道歉

【问题讨论】:

    标签: vba range comments


    【解决方案1】:

    您可以使用 Match 功能检查范围是否包含字符串并设置消息:

    pos = 0
    On Error Resume Next
    pos = Application.WorksheetFunction.Match("final bottling", Ip.Range("C9:H9"), 0)
    ' if not match (case insensitive), then error occurs and pos remains 0; if match was successful, pos = 1+
    Op1.Cells(8, 5) = IIf(pos = 0, "", "Final Bottling Run, Please Consume materials. If unsure, check with materials planner!")
    On Error GoTo 0 
    

    另一种方法是在单元格 Opt!E8 中使用这样的公式:

    =IFERROR(IF(MATCH("final bottling",Ip!C9:H9,0)>0,"Final Bottling 运行,请消耗材料。如果不确定,检查材料 规划师!",""),"")

    或者使用 Ip.Range("C9:H9").Find() 函数

    【讨论】:

      【解决方案2】:

      如果 Ip 中 C9:H9 中的任何单元格具有“最终装瓶”,这将显示消息

      Sub Test()
          Dim i As Long
          Dim checkCell As Range
          
          Set checkCell = op1.Cells(8, 5)
          
          For i = 3 To 8
              Debug.Print LCase(Left$(Ip.Cells(9, i).Value, 14))
              If LCase(Left$(Ip.Cells(9, i).Value, 14)) = "final bottling" Then
                  checkCell.Value = "Final Bottling Run, Please Consume materials. If unsure, check with materials planner!"
                  Exit Sub 'Check is complete, exit sub from here
              End If
          Next i
          
          checkCell.Value = vbNullString 'code will only pass here if it fails all the check in the loop above.
      End Sub
      

      【讨论】:

      • 谢谢!像魅力一样工作!
      猜你喜欢
      • 2023-01-20
      • 2020-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-07
      • 1970-01-01
      • 2016-11-06
      • 2023-03-27
      相关资源
      最近更新 更多