2015-07-10 54 views
0

我是一名VBA新手,因此在此先感謝任何能夠幫助我的人。基本上,我使用的是一種改編的Ron de Bruin代碼,當他們的出勤低於特定excel單元格顯示的特定級別時,會自動發送郵件給學生。到目前爲止,這麼好,Ron de Bruin的東西照顧這個。 但還有另一個標準,我想添加,基本上只發送郵件,如果在出席的同一行的不同單元格中也有字母'Y'。總之,我只希望郵件能夠滿足兩個標準的人,1)低於一定水平,2)在另一個單元中有'Y',但目前的代碼只佔第一個標準。非常感謝。阿倫(下面的代碼)根據多個條件從excel發送一封自動郵件

Option Explicit 

Private Sub Worksheet_Calculate() 


Dim FormulaRange As Range 
Dim NotSentMsg As String 
Dim MyMsg As String 
Dim SentMsg As String 
Dim MyLimit As Double 



NotSentMsg = "Not Sent" 
SentMsg = "Sent" 

'Above the MyLimit value it will run the macro 
MyLimit = 80 

'Set the range with Formulas that you want to check 
Set FormulaRange = Me.Range("BH279:BH280") 

On Error GoTo EndMacro: 
For Each FormulaCell In FormulaRange.Cells 
    With FormulaCell 
     If IsNumeric(.Value) = False Then 
      MyMsg = "Not numeric" 
     Else 
      If .Value < MyLimit Then 


       MyMsg = SentMsg 
       If .Offset(0, 1).Value = NotSentMsg Then 
        Call Mail_with_outlook2 
       End If 
      Else 
       MyMsg = NotSentMsg 
      End If 
     End If 
     Application.EnableEvents = False 
     .Offset(0, 1).Value = MyMsg 
     Application.EnableEvents = True 
    End With 
Next FormulaCell 

ExitMacro: 
Exit Sub 

EndMacro: 
Application.EnableEvents = True 

MsgBox "Some Error occurred." _ 
    & vbLf & Err.Number _ 
     & vbLf & Err.Description 

End Sub 
+0

你可以用'IF(AAA111 =「Y」,百分比,1)'序言來修正你的公式。如果你想提高你的知識,而不是解決這個問題,我會推薦'家庭作業'標籤(不確定拼寫)。 – user3819867

+0

非常感謝理查德,我會在星期一嘗試一下,讓你知道。如果您曾經擔任公職,您可以放心支持我! – Alun

+0

理查德謝謝,即時嘗試下面的答案,因爲我的知識是如此有限,我不知道如何使用您的建議,在哪裏粘貼中頻(AAA111 =「Y」,百分比,1)以及我需要添加什麼它工作。如果你能詳細說明,我會永遠感激。 Alun – Alun

回答

0
If .Value2 < MyLimit And Not .EntireRow.Find(What:="Y", LookIn:=xlValues, LookAt:=xlWhole) Is Nothing Then Call Mail_with_outlook2 
'If you look for the complementer solution, remove the " Not" 

您要查找的同一行中的值"Y"
我建議設置輸出變量以及郵件宏Call Mail_with_outlook2(emailaddress, name, title, MyValue)

+0

嗨用戶3819867,感謝密爾爲此。我必須等到週一試試它,但我相信它會起作用。我會告訴你。非常感謝。週末愉快。再次 – Alun

+0

嗨,我 粘貼你的代碼代替 如果.value的 Alun

+0

我的語法只是'調用'迭代在同一行。我相信這可能會導致你的問題。 '如果.Value2 user3819867