消息不会显示一次

时间:2018-06-08 14:16:08

标签: vba excel-vba excel

我不明白为什么当数据插入错误超过2次(即A列)时多次重复出现错误消息。因此,如果A列中有3个错误,则消息弹出3次。我将Mwarning设置为布尔值,但我猜它没有正确设置。

有人可以指导我解决这个问题吗?

    Dim aRec As Worksheet, bRec As Worksheet, wb As Workbook
    Dim match
    Dim Mwarning As Boolean
    Dim c as long


    Set wb = Excel.ActiveWorkbook
    Set aRec = wb.Worksheets(1)
    Set bRec = wb.Worksheets(2)

    Application.ScreenUpdating = False

    For c = 2 To aRec.Cells(Rows.Count, "A").End(xlUp).Row
        match = Application.match(aRec.Cells(c, 1).Value, bRec.Columns(2), 0)

        If IsError(match) And Not IsEmpty(aRec.Cells(c, 1)) Then
             aRec.Cells(c, 1).Interior.Color = RGB(255, 0, 0)
             MsgBox "Mistakes are in column A", vbInformation, "IMPORTANT:"
             Mwarning = True
        Else
            aRec.Cells(c, 1).Interior.Color = RGB(255, 255, 255)

        End If

    Next c

    If Mwarning = True Then MsgBox "No errors found!", vbInformation, "IMPORTANT:" ' if an error is set in column A, then do nothing, otherwise get the next message


    Application.ScreenUpdating = True

4 个答案:

答案 0 :(得分:1)

在循环MsgBox "Mistakes are in column A", vbInformation, "IMPORTANT:"中添加msgbox函数 所以我认为如果遇到IF条件,每次都会重复msgbox。 删除它。

答案 1 :(得分:1)

编辑:改进的答案,单独留下细胞着色而不添加另一个IF。 Msg只会触发一次,因为错误字符串是在循环内部构建的,所以Row位置将在单个msgbox中报告,如果有很多错误,请小心。

Dim aRec As Worksheet, bRec As Worksheet, wb As Workbook
Dim match
Dim Mwarning As Boolean
Dim c As Long
Dim rpt As String


Set wb = Excel.ActiveWorkbook
Set aRec = wb.Worksheets(1)
Set bRec = wb.Worksheets(2)

Application.ScreenUpdating = False

Mwarning = False 'Always, Always, Always set an intitial condition
rpt = "WARINING: Column A Errors are Found" & vbNewLine & vbNewLine

For c = 2 To aRec.Cells(Rows.Count, "A").End(xlUp).Row
    match = Application.match(aRec.Cells(c, 1).Value, bRec.Columns(2), 0)

'Added the piece that if there is an error, check if the box has already fired = true,
'if it has NOT fired, then fire it once and not again
    If IsError(match) And Not IsEmpty(aRec.Cells(c, 1)) Then
         aRec.Cells(c, 1).Interior.Color = RGB(255, 0, 0)
         Mwarning = True
         rpt = rpt & "Error Found at Row: " & c & vbNewLine
    Else
        aRec.Cells(c, 1).Interior.Color = RGB(255, 255, 255)
    End If

Next c

'You only want this to pop if no errors are found in the FOR LOOP
If Mwarning = False Then
    'if no error was found report this message
    MsgBox "No errors found!", vbInformation, "IMPORTANT:"
Else 'error was found in column A so Mwarning is true
    MsgBox rpt, vbInformation, "IMPORTANT:"

End If
Application.ScreenUpdating = True

答案 2 :(得分:1)

编辑: 用这样的日志替换循环中的msgbox,并在子结尾添加一个msgbox:

Sub testMsg()

   Dim aRec As Worksheet, bRec As Worksheet, wb As Workbook
    Dim match
    Dim c As Long


    Set wb = Excel.ActiveWorkbook
    Set aRec = wb.Worksheets(1)
    Set bRec = wb.Worksheets(2)

    Application.ScreenUpdating = False

    Dim log As String
    log = ""

    For c = 2 To aRec.Cells(Rows.Count, "A").End(xlUp).Row
        match = Application.match(aRec.Cells(c, 1).Value, bRec.Columns(2), 0)

        If IsError(match) And Not IsEmpty(aRec.Cells(c, 1)) Then
             aRec.Cells(c, 1).Interior.Color = RGB(255, 0, 0)
             log = "Mistakes are in column A"
        Else
            aRec.Cells(c, 1).Interior.Color = RGB(255, 255, 255)

        End If

    Next c

    If log = "" Then
    MsgBox "No errors found!", vbInformation, "IMPORTANT:" '
    Else
    MsgBox log, vbInformation, "IMPORTANT:"


    Application.ScreenUpdating = True

End Sub

在循环MsgBox "Mistakes are in column A", vbInformation, "IMPORTANT:"中添加msgbox函数 所以我认为如果遇到IF条件,每次都会重复msgbox。

答案 3 :(得分:0)

您已将Mwarning设置为布尔值,并在找到错误时将其设置为True,但您不会在循环中检查其值。因此,循环中的if也应检查Mwarning值。