這是我第一個問題,它是由於looong嘗試而導致的,並且未能通過我自己或通過在線研究來解決。在Excel中刪除VBA末尾的單元格內容
我寫了一個代碼(即使用我在網上找到的那些),以使從Excel發送的自動化的電子郵件時「是」被寫在列M.
請看以下代碼:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim KeyCells As Range
Set KeyCells = Range("M2:M1000")
If Not Application.Intersect(KeyCells, Range(Target.Address)) _
Is Nothing Then
Call SendmailAuto
End If
End Sub
而且
Sub SendmailAuto()
Dim OutApp As Object
Dim OutMail As Object
Dim RelDate As Range
Dim lastRow As Long
Dim dateCell, dateCell1 As Date
Application.ScreenUpdating = False
Set OutApp = CreateObject("Outlook.Application")
OutApp.Session.Logon
lastRow = Range("A" & Rows.Count).End(xlUp).Row
On Error GoTo cleanup
For Each RelDate In Range("K2:K" & lastRow)
If RelDate = "" Then GoTo 1
dateCell = RelDate.Value
dateCell1 = Cells(RelDate.Row, "I").Value
SendMailCell = Cells(RelDate.Row, "M").Value
If SendMailCell = "YES" Then
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
.To = Cells(RelDate.Row, "AD").Value & ";" & Cells(RelDate.Row, "AE").Value & ";" & Cells(RelDate.Row, "AF").Value
.subject = "A new submission deadline set"
.body = "Dear " & Cells(RelDate.Row, "R").Value & "," _
& vbNewLine & vbNewLine & _
"A new submission deadline for " & Cells(RelDate.Row, "G").Value & _
" is now set to " & dateCell & " by " & Cells(RelDate.Row, "L").Value _
& vbNewLine & vbNewLine _
& vbNewLine & vbNewLine & _
"Regards," & vbNewLine & _
"XYZ"
.send
End With
On Error GoTo 0
Set OutMail = Nothing
End If
1: Next RelDate
cleanup:
Set OutApp = Nothing
Application.ScreenUpdating = True
End Sub
這一切工作正常,但我的問題是,萬一有在M列中一個以上的「YES」 ES,多個電子郵件發送。我希望在代碼中有一行可以在每封電子郵件後清除M列的內容。
換句話說 - 我寫YES,電子郵件發送,YES消失。我在下一行輸入YES,發送電子郵件,這個YES也消失了。
我試過所有Clear.Contents,Delete.Contents和所有這些,但似乎沒有任何工作。它要麼崩潰,要麼繼續刪除M上的所有列。
謝謝大家提前!
年底之前謝謝,這有助於增加該指令!我確信我是在錯誤的地方做的,因爲我一直把它放在整個劇本的末尾! – Nick37
你可以驗證答案嗎? – h2so4