【问题标题】:Highlight mismatched entries in single dynamic column and present msgbox for entire range突出显示单个动态列中不匹配的条目并显示整个范围的 msgbox
【发布时间】:2013-12-03 19:44:42
【问题描述】:

我是新来的,对 VBA 比较陌生,所以请多多包涵。我四处寻找这个问题的答案,但找不到任何东西,所以如果这个问题已经在其他地方得到回答但我没有找到,我深表歉意。

我想搜索动态长度的指定列并用数字系统替换人口统计数据(下面的替换代码可以正常工作,但如果您有与效率相关的建议,请务必继续!)。然后我想要发生的是突出显示任何与数字不匹配的条目 - 这些将是字符串,例如,“经理”而不是“老板”或类似的东西 - 并弹出一个消息框请求用户在突出显示的字段中手动编码。

目前正在发生的事情是我对任何不匹配的条目都有条件格式,因此它们会被突出显示。我的“对于每个单元格”为其找到的每个单独条目填充一个消息框,但我只想要一个用于整个范围的消息框。通过 VBA 突出显示不匹配的条目会更好吗?如何?我怎样才能将这个编码为只为整个范围提供一个消息框?

提前感谢您的帮助!

Sub ReplaceRaterDemographicCodes()
'Find and replace demographics with their corresponding codes.
Columns("H:H").Select
    With Selection
        .Replace What:="Self", Replacement:="78"
        .Replace What:="Boss", Replacement:="74"
        .Replace What:="Boss 1", Replacement:="74"
        .Replace What:="Peer", Replacement:="75"
        .Replace What:="Direct Report", Replacement:="76"
        .Replace What:="Customer", Replacement:="77"
        .Replace What:="Other", Replacement:="79"
        .Replace What:="Boss 2", Replacement:="72"
        .Replace What:="Boss 3", Replacement:="73"
    End With
    For Each Cell In Range("H2:H" & Range("H" & Rows.Count).End(xlUp).Row).Select
        If Not Cell.Value = 72 And Not Cell.Value = 73 And _
        Not Cell.Value = 74 And Not Cell.Value = 75 And Not Cell.Value = 76 And _
        Not Cell.Value = 77 And Not Cell.Value = 78 And Not Cell.Value = 79 And _
        Not Cell.Value = "" Then
            MsgBox ("There are uncommon demographics listed. Please modify as needed.")
        End If
    Next Cell
End Sub

【问题讨论】:

    标签: excel vba msgbox


    【解决方案1】:

    由于您实际上并不需要遍历所有内容 - 直到您知道要显示消息框,您可以在显示消息框后退出 for 循环:

    Sub ReplaceRaterDemographicCodes()
    ...
    
    For Each Cell In Range("H2:H" & Range("H" & Rows.Count).End(xlUp).Row).Select
        If Not Cell.Value = 72 And Not Cell.Value = 73 And _
        Not Cell.Value = 74 And Not Cell.Value = 75 And Not Cell.Value = 76 And _
        Not Cell.Value = 77 And Not Cell.Value = 78 And Not Cell.Value = 79 And _
        Not Cell.Value = "" Then
            MsgBox ("There are uncommon demographics listed. Please modify as needed.")
            Exit For
        End If
    Next Cell
    End Sub
    

    这样消息框只会显示一次,并且仅在符合您的条件时才显示。

    【讨论】:

    • 感谢您的快速回复。两件事:1)当我尝试“Dim showbox As Boolean = False”时出现编译错误,所以我尝试了两行,一条是 Dim showbox As Boolean”,然后是“showbox = False”。这是正确的吗?2)我是当我使用您的代码时出现错误“类型不匹配”,但它没有打开 VBA 调试器。您建议的概念是有道理的,但我不知道为什么它不起作用。
    • 抱歉,我刚刚更新了我的答案。我习惯于 vb.net 而不是 vba,所以我不确定是什么导致了你的错误。无论如何,我的更新答案会更好,因为您可以在第一次满足条件后停止 for 循环。
    • 啊哈!我发现了错误。 For Each 末尾的 .Select 正在抛出 vba 循环(我仍然不知道为什么......)。只需删除它,代码就可以很好地工作!我添加了一个Else MsgBox ("All clear!") 以帮助确保一切正常并且一切看起来都很棒。感谢您的帮助 =D!
    【解决方案2】:

    这里与@jgridley 一起提供了一个答案,其中包括一个MsgBox 表示“全部清除!”。 .Select For Each 行末尾的 .Select 也被删除(因为这会导致错误)。以红色突出显示的单元格使用条件格式完成。

    Sub ReplaceRaterDemographicCodes()
    
    Dim bAllClear As Boolean
    bAllClear = True
    
    ...
    
    For Each Cell In Range("H2:H" & Range("H" & Rows.Count).End(xlUp).Row)
        If Not Cell.Value = 72 And Not Cell.Value = 73 And _
        Not Cell.Value = 74 And Not Cell.Value = 75 And Not Cell.Value = 76 And _
        Not Cell.Value = 77 And Not Cell.Value = 78 And Not Cell.Value = 79 And _
        Not Cell.Value = "" Then
            bAllClear = False
    Exit For
    End If
    Next Cell
    
    If bAllClear = True Then
        MsgBox ("All clear!")
        Else
        MsgBox ("There are uncommon demographics listed." & vbNewLine & _
            vbNewLine & "Please modify the cells highlighted in red.")
    End If
    End Sub
    

    【讨论】:

      猜你喜欢
      • 2021-12-22
      • 2014-10-28
      • 2017-06-23
      • 2016-07-14
      • 2022-01-27
      • 1970-01-01
      • 1970-01-01
      • 2012-11-03
      • 2023-01-08
      相关资源
      最近更新 更多