下面的代码是在VBA中使用if elseif语句发送电子邮件提醒。接收者将收到有关该物品需要进行测试或目视检查的电子邮件通知。该代码将计算到期日的剩余天数,例如 30 天。当项目到期日为 30 天时,vba 将生成电子邮件并自动转发给接收者,并在每一行上指示“是”或“正确”。但代码中有一个错误(Else without If)。请指教如何调试它。谢谢你
Sub datesexcelvba()
Dim Outlook As Object, EMail As Object
Set Outlook = CreateObject("Outlook.Application")
Set EMail = Outlook.CreateItem(0)
Dim mydate1 As Date
Dim mydate2 As Long
Dim mydate3 As Date
Dim mydate4 As Long
Dim datetoday1 As Date
Dim datetoday2 As Long
Dim datetoday3 As Date
Dim datetoday4 As Long
Dim x As Long
Lastrow = Sheets("Lift equipment1").Cells(Rows.Count, 1).End(xlUp)
For x = 3 To Lastrow
mydate1 = Cells(x, 16).Value
mydate3 = Cells(x, 11).Value
mydate2 = mydate1
mydate4 = mydate3
Cells(x, 21).Value = mydate2
Cells(x, 25).Value = mydate4
datetoday1 = Date
datetoday2 = datetoday1
datetoday3 = Date
datetoday4 = datetoday3
Cells(x, 22).Value = datetoday2
Cells(x, 26).Value = datetoday4
If mydate2 - datetoday2 = 30 Then
EMail.To = Cells(x, 20).Value
With EMail
.Subject = Cells(x, 18).Value
.Body = Cells(x, 18).Value
.Display
'.Send
ElseIf mydate4 - datetoday4 = 30 Then
EMail.To = Cells(x, 20).Value
With EMail
.Subject = Cells(x, 19).Value
.Body = Cells(x, 19).Value
.Display
'.Send
Else
End If
End With
Cells(x, 23) = "Yes"
Cells(x, 23).Interior.ColorIndex = 3
Cells(x, 24).Value = mydate2 - datetoday2
Cells(x, 27) = "True"
Cells(x, 27).Interior.ColorIndex = 3
Cells(x, 28).Value = mydate4 - datetoday2
End If
Next
Set EMail = Nothing
Set Outlook = Nothing
End Sub`
如果您正确缩进了代码(如下所示),您的代码中的错误将更容易看出。
当下面的代码编译时,我不能说它符合您对代码的意图(您需要测试并检查)另外,确保
If True Then ' this 'If' was missing and needs to be fixed
行中的“If”语句需要修复,因为它在您的代码中丢失了已发布。
Sub datesexcelvba()
Dim Outlook As Object, EMail As Object
Set Outlook = CreateObject("Outlook.Application")
Set EMail = Outlook.CreateItem(0)
Dim mydate1 As Date
Dim mydate2 As Long
Dim mydate3 As Date
Dim mydate4 As Long
Dim datetoday1 As Date
Dim datetoday2 As Long
Dim datetoday3 As Date
Dim datetoday4 As Long
Dim x As Long
lastRow = Sheets("Lift equipment1").Cells(Rows.Count, 1).End(xlUp)
For x = 3 To lastRow
mydate1 = Cells(x, 16).Value
mydate3 = Cells(x, 11).Value
mydate2 = mydate1
mydate4 = mydate3
Cells(x, 21).Value = mydate2
Cells(x, 25).Value = mydate4
datetoday1 = Date
datetoday2 = datetoday1
datetoday3 = Date
datetoday4 = datetoday3
Cells(x, 22).Value = datetoday2
Cells(x, 26).Value = datetoday4
If mydate2 - datetoday2 = 30 Then
EMail.To = Cells(x, 20).Value
With EMail
If True Then ' this 'If' was missing and needs to be fixed
.Subject = Cells(x, 18).Value
.Body = Cells(x, 18).Value
.Display
'.Send
ElseIf mydate4 - datetoday4 = 30 Then
EMail.To = Cells(x, 20).Value
'With EMail
.Subject = Cells(x, 19).Value
.Body = Cells(x, 19).Value
.Display
'.Send
'Else
End If
End With
Cells(x, 23) = "Yes"
Cells(x, 23).Interior.ColorIndex = 3
Cells(x, 24).Value = mydate2 - datetoday2
Cells(x, 27) = "True"
Cells(x, 27).Interior.ColorIndex = 3
Cells(x, 28).Value = mydate4 - datetoday2
End If
Next
Set EMail = Nothing
Set Outlook = Nothing
End Sub