简体   繁体   English

合并两个Worksheet_Change子

[英]Combine two Worksheet_Change Subs

I need two actions to take place on one sheet in my workbook. 我需要在工作簿的一张纸上进行两项操作。 Both are based on a change event, but do not know how to make them both work. 两者都基于更改事件,但是不知道如何使它们都起作用。 Below is the codes that I have: 以下是我拥有的代码:

Private Sub Worksheet_Change(ByVal Target As Range)

Dim r As Range
Set r = Target.EntireRow

    If Target.row = 1 Then Exit Sub ' Don't change header color

    If r.Cells(1, "AD").Value <> "" Then
        r.Font.Color = RGB(0, 176, 80)
    Else
        r.Font.ColorIndex = 1
    End If
End Sub

And this one: 还有这个:

Private Sub Worksheet_Change(ByVal Target As Range)

Dim r As Range
Set r = Target.EntireRow

    If Target.row = 1 Then Exit Sub ' Don't change header color

    If r.Cells(1, "E").Value = "6" Then
        r.Font.Color = RGB(255, 0, 0)
    Else
        r.Font.ColorIndex = 1
    End If
End Sub

To help determine the best course of action, here is what the end results must be: 为了帮助确定最佳的行动方案,这是最终结果必须是的:

For any row that has a date entered into cell AD, the text color for the entire row should change to green. 对于在单元格AD中输入日期的任何行,整个行的文本颜色应更改为绿色。 However, if cell E of any row contains a 6 (this is a number formatted as text), then the text in that row should be red. 但是,如果任何行的单元格E包含6(这是一个格式化为文本的数字),则该行中的文本应为红色。

I am sure that I am over thinking this. 我敢肯定,我正在考虑这个问题。 All suggestions are appreciated. 所有建议表示赞赏。

Use an And in your first If statement, and add an ElseIf statements. 在第一个If语句中使用And ,然后添加ElseIf语句。

I am not exactly sure what you want to take precedence if both a date and 6 exist or if there is one without the other, but you can easily adjust the If Then ElseIf block below to sort out your needs. 我不确定如果日期和6都存在,或者日期中没有日期,那么您想优先考虑什么,但是您可以轻松地调整下面的If Then ElseIf块来解决您的需求。

If r.Cells(1, "AD").Value <> "" And r.cells(1,"E").Value = "6" Then
    r.Font.Color = RGB(255, 0, 0)
ElseIf r.Cells(1,"AD").Value <> "" Then 
    r.Font.Color = RGB(0, 176, 80)
Else
    r.Font.ColorIndex = 1
End If

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

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