简体   繁体   English

在表格中逐行“直到”直到在VBA中找到第一个有色单元格?

[英]'Do-until' in a table row by row until the first colored cell found in VBA?

I have a table in excel with 7 columns users can place their inputs. 我在excel中有一张表格,其中有7列,用户可以放置其输入。 After placing their inputs, they have a validation macro to run, which colors the cells with invalid values to red. 放置输入后,它们将运行验证宏,该宏会将无效值的单元格颜色设置为红色。 I want to create a summary sheet about these errors after this coloring with VBA macro, but only if there are any colored cell in the table. 我想在使用VBA宏着色后创建关于这些错误的摘要表,但前提是表格中有任何着色的单元格。 If there is at least one red cell somewhere in the table range, this summary sheet should be created, else a MsgBox should pop up telling that there were no errors. 如果表范围内某处至少有一个红色单元格,则应创建此摘要表,否则应弹出一个MsgBox告知没有错误。 I want to create the easiest way to do that, something like this: The problem is with this I got the "Validation errors found, please check the Errors sheet" message, even if I do not have red cells. 我想创建一种最简单的方法来执行以下操作:问题是与此相关的问题,即使我没有红色单元格,我也收到“发现验证错误,请检查错误表”消息。

Sub errorListCreation(Sheet1 As Worksheet)
Dim isColored As Boolean
isColored = False
    For Each Acell In Sheet1.Range("A2", Range("K" & Sheet1.usedRange.Rows.Count))
        With Acell
            If Acell.DisplayFormat.Interior.Color = RGB(255, 0, 0) Then
                isColored = True
                Exit For
            End If
        End With
    Next Acell

    If isColored = True Then
        MsgBox "Validation errors found, please check the Errors sheet. "
        For Each errorList In Worksheets
            If errorList.Name = "Errors" Then
                Application.DisplayAlerts = False
                Sheets("Errors").Delete
                Application.DisplayAlerts = True
            End If
        Next
        Sheets.Add(After:=Sheets(Sheets.Count)).Name = "Errors"
    Else: MsgBox "Validation complete, please check the reconciliation sheet."
    End If
End Sub

在此处输入图片说明

I suggest something like that: 我建议是这样的:

Option Explicit

Public Function IsColored(ByVal Rng As Range) As Boolean
    IsColored = (Rng.Interior.Color = RGB(255, 0, 0))
End Function

Public Sub ColorFinder(ByVal ws As Worksheet)
    Dim ErrorFound As Boolean

    Dim InputTable As ListObject
    Set InputTable = ws.ListObjects("Table4") 'probably no good idea to hard code the table name (at least use a meaningful name)

    With InputTable.DataBodyRange
        Dim iRow As Long, iCol As Long
        For iRow = .Rows.Count To 1 Step -1
            For iCol = .Columns.Count To 1 Step -1
                If IsColored(.Cells(iRow, iCol)) Then
                    ErrorFound = True
                    Exit For
                End If
            Next iCol
            If ErrorFound Then Exit For
        Next iRow
    End With

    If ErrorFound Then
        MsgBox "Errors found", vbCritical
    Else
        MsgBox "No errors found", vbInformation
    End If
End Sub

Sub test()
    ColorFinder Worksheets("Sheet1")
End Sub

On the following sheet 在下一张纸上

在此处输入图片说明 Image 1: Assume the above worksheet "Sheet1" with a ListObject "Table4". 图1:假设上面的工作表“ Sheet1”和一个ListObject“ Table4”。

you will get 你会得到

"Errors found" “发现错误”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM