如果单元格是红色的,它会生成一个消息框,但如果不是,它会调用运行另一个宏

发布于 2025-01-16 07:29:41 字数 1638 浏览 1 评论 0原文

我正在尝试构建一个工作簿,如果某个单元格范围具有 0 值,则该工作簿设置为有条件地格式化,该范围是动态的,具体取决于在任何给定日期输入到工作表中的数据。所以,我想我可以有条件地将单元格格式化为在单元格为 0 的情况下变成红色。我通过执行此宏来做到这一点

Sub ColorTACsErrorCheck()
'
' Checks for any TACs Errors with 0 value
'

'
    Sheets("TACS Data").Select
    Range("N2").Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, _
        Formula1:="=0"
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    With Selection.FormatConditions(1).Font
        .Color = vbRed
        .TintAndShade = 0
    End With
    With Selection.FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .Color = vbRed
        .TintAndShade = 0
    End With
    Selection.FormatConditions(1).StopIfTrue = False
End Sub

它工作完美,并且适用于动态范围。

我尝试使用下面的代码让它在其中一个单元格是红色的情况下生成一条消息,它确实如此,但我希望它不运行我为工作簿的其余部分所拥有的实际宏,如果它有一个红色单元格。然而,它似乎无论如何都会调用宏。

Sub Message()
'
' Message
'

'
    Sheets("TACS Data").Select
    Dim oneCell As Range, newNote As String
    For Each oneCell In Sheets("TACS Data").Range("N3:N100")
    With oneCell
        If oneCell.DisplayFormat.Interior.Color = vbRed Then
            MsgBox "A carrier did not clock to the street. Please correct this error and rerun Employee Moves report in TACs."
            If newNote = "False" Then
            Exit Sub
        Else: Call Run
        End If
        End If
    End With
    Next oneCell
End Sub

我试图使工作簿尽可能用户友好,以便不懂 Excel 的人只需点击“运行”,如果没有错误,它会运行整个报告以获得结果,但如果有错误它只是告诉他们基本上再试一次。

这可能吗?或者我应该在“运行报告”按钮之前点击一个“检查错误”按钮?

I am trying to build a workbook that is set to conditionally format if a certain range of cells has a 0 value, the range is dynamic based on what data is entered into the sheet on any given day. So, I thought I could conditionally format the cells to turn red in the event a cell has 0. I did this by doing this macro

Sub ColorTACsErrorCheck()
'
' Checks for any TACs Errors with 0 value
'

'
    Sheets("TACS Data").Select
    Range("N2").Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, _
        Formula1:="=0"
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    With Selection.FormatConditions(1).Font
        .Color = vbRed
        .TintAndShade = 0
    End With
    With Selection.FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .Color = vbRed
        .TintAndShade = 0
    End With
    Selection.FormatConditions(1).StopIfTrue = False
End Sub

It works perfectly, and works with dynamic ranges.

I tried using the below code to have it generate a message if one of the cells is red, which it does, but I want it to not run the actual macros I have for the rest of the workbook IF it has a red cell. However, it seems to call the macro regardless.

Sub Message()
'
' Message
'

'
    Sheets("TACS Data").Select
    Dim oneCell As Range, newNote As String
    For Each oneCell In Sheets("TACS Data").Range("N3:N100")
    With oneCell
        If oneCell.DisplayFormat.Interior.Color = vbRed Then
            MsgBox "A carrier did not clock to the street. Please correct this error and rerun Employee Moves report in TACs."
            If newNote = "False" Then
            Exit Sub
        Else: Call Run
        End If
        End If
    End With
    Next oneCell
End Sub

I'm trying to make the workbook as user friendly as possible so that people who don't know Excel only have to hit "Run" and if there are no errors, it runs the entire report to get results, but if there is an error it simply tells them to try again basically.

Is this possible? Or should I just have a "check for errors" button that they have to hit before the "run report" button?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文