我如何为一个总是会增加值的列写一个vba

时间:2014-01-21 12:38:07

标签: excel vba excel-vba outlook-vba conditional-formatting

我有一个Excel电子表格,其中我输入了一列,每天发动机小时数。我需要设置一个VBA代码,它将作为一个警告系统来提供引擎服务。我希望单元格在300处变为琥珀色,在400处变为深橙色,在500处变为红色。我还需要它在500处生成电子邮件。我遇到的问题是列总是会增加值。电子邮件在500发送后,需要重置为800,900和1000,依此类推。这有可能吗?

提前致谢。 微量

1 个答案:

答案 0 :(得分:0)

根据我之前的评论,您可以使用基于公式的条件格式来完成此任务:

enter image description here

要自动发送电子邮件,您可以根据您的规范修改以下VBA:

Private Sub Worksheet_Change(ByVal Target As Range)
Dim oApp As Object
Dim oMail As Object
If Target.Column = 1 Then 'If the change was made in your entry column
    If Target.Value Mod 500 = 0 Then
        Set oApp = CreateObject("Outlook.Application")
        Set oMail = oApp.CreateItem(0)
            With oMail
                .To = "YouRecipient@TheirDomain.com, Sample@Gmail.com"
                .Subject = "Engine Service Needed"
                .Body = "The engine mileage is currently at " _
                        + CStr(Target.Value) _
                        + " Miles And Needs to be Serviced"
                .Display 'You can replace the word display with the word send if you don't want to preview
            End With
    End If
End If
End Sub

从评论中更新:

For Amber change the formula to `=MOD(A1,500)>0`
For darker orange use `=MOD(A1,500)>300`
And for Red use `=OR(MOD(A1,500)>400,MOD(A1,500)=0)`