【问题标题】:MsgBox if a cell value contains partial text如果单元格值包含部分文本,则为 MsgBox
【发布时间】:2019-08-01 07:35:03
【问题描述】:

我设置了一个工作表,其中在 M 列中输入了一个位置,但是它需要阻止人们使用缩写 - 我正在尝试设置一个 MsgBox,例如,如果在一个M5:M1000 范围内的单元格但似乎无法使其工作?我认为这将是一个私人潜艇,如下所示?

编辑:它需要搜索部分值,以便在完全输入文本后检查单元格。

Private Sub IncorrectText()

    If InStr(ActiveSheet.Range("M5:M1000").Value, "J/W") > 0 Then

        MsgBox "Please DO NOT use abbreviations" & vbNewLine & vbNewLine & "Change this to 'at/ from the junction with...'"

    End If

End Sub

【问题讨论】:

  • 您无法以这种方式从范围中获取值。您可以循环遍历单元格或使用 Change 事件仅检测已更改的单元格。
  • 或为此目的使用Range.Find。但更好的是Worksheet_SelectionChange 事件来捕获单元格更改

标签: excel vba


【解决方案1】:

在编辑单元格后立即使用Worksheet.Change event 检查输入。如果输入了一些不允许的内容,则使用Application.Undo 撤消。

Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim AffectedRange As Range
    Set AffectedRange = Intersect(Target, Me.Range("M5:M1000"))  'check if change happened in the desired range

    If Not AffectedRange Is Nothing Then
        Dim Cell As Range
        For Each Cell In AffectedRange
            Select Case True
                Case InStr(1, Cell.Value, "J/W") > 0
                    MsgBox "do not use 'J/W'"
                    Application.Undo
                    Exit For

                Case InStr(1, Cell.Value, "c/o") > 0
                    MsgBox "do not use 'c/o'"
                    Application.Undo
                    Exit For
            End Select
        Next Cell
    End If
End Sub

图 1:如果插入了禁止的缩写之一,则撤消。

如果您不需要单独的消息框,您可以使用它。所以你可以把所有的缩写放到一个列表中:

Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim AffectedRange As Range
    Set AffectedRange = Intersect(Target, Me.Range("M5:M1000"))  'check if change happened in the desired range

    Dim NotAllowedList() As Variant
    NotAllowedList = Array("c/o", "J/W")

    If Not AffectedRange Is Nothing Then
        Dim FoundNotAllowed As Boolean
        Dim Cell As Range, NotAllowed As Variant
        For Each Cell In AffectedRange
            For Each NotAllowed In NotAllowedList
                If InStr(1, Cell.Value, NotAllowed, vbTextCompare) > 0 Then
                    FoundNotAllowed = True
                    Exit For
                End If
            Next NotAllowed

            If FoundNotAllowed Then Exit For
        Next Cell

        If FoundNotAllowed Then
            MsgBox "do not use abbreviations"
            Application.Undo
        End If
    End If
End Sub

【讨论】:

  • 这太好了,谢谢 - 有没有办法检查它与大小写不符?即可以说“J/W”还是“j/w”?
  • @ffc2004 是的,使用InStr(1, Cell.Value, "J/W", vbTextCompare) 使其不区分大小写。
  • @ffc2004 如果您不需要单独的消息,请查看我的编辑。
  • 道歉 - 另一个快速的,有没有办法在同一行中包含多个字符串?例如Case InStr(1, Cell.Value, "KT1" OR "KT2", vbTextCompare) > 0
  • 那行不通。您需要将InStrInStr(1, Cell.Value, "KT1", vbTextCompare) > 0 Or InStr(1, Cell.Value, "KT2", vbTextCompare) > 0 之类的OR 连接起来,或者列出用逗号分隔的案例,例如Case InStr(1, Cell.Value, "KT1", vbTextCompare) > 0, InStr(1, Cell.Value, "KT2", vbTextCompare) > 0
【解决方案2】:

这可以在没有 VBA 的情况下通过使用如下公式的自定义数据验证来实现:

=IF(ISNUMBER(FIND("J/W";M5;1));True())

【讨论】:

    【解决方案3】:

    我认为您必须将逻辑放入工作表事件中。 在 VBE 编辑器中,双击用户将输入数据的工作表的名称。 在出现的工作表模块中,使用如下所示的选择更改事件:

    Private Sub Worksheet_SelectionChange(ByVal Target As Range) 
    
     If Not IsError(Application.Match("J/W", Range("M5:M1000"), 0)) Then msgbox("write your message here")
    
    End Sub
    

    现在如果有人输入 J/w 广告,按 Enter 就会弹出 MSGbox。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-09-27
      • 2020-08-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多