很少使用VBA。返回到我在一段时间前设置的自动合规性电子邮件宏。想让自己重新了解它的工作原理,并立即遇到此错误。自创建以来没有更改任何代码,我不确定是什么导致了错误。
For i = 1 To xLastRow
xRgDateVal = ""
xRgDateVal = xRgDate.Offset(i - 1).Value
If xRgDateVal <> "" Then
If CDate(xRgDateVal) - Date <= -1820 Then
xRgSendVal = xRgSend.Offset(i - 1).Value
xRgNameVal = xRgName.Offset(i - 1).Value
xMailSubject = xRgText.Offset(i - 1).Value & " on " & xRgDateVal
vbCrLf = "<br><br>"
xMailBody = "<HTML><BODY>"
xMailBody = xMailBody & "Dear " & xRgNameVal & vbCrLf
xMailBody = xMailBody & xRgText.Offset(i - 1).Value & vbCrLf
xMailBody = xMailBody & "</BODY></HTML>"
Set xMailItem = xOutApp.CreateItem(0)
With xMailItem
.Subject = xMailSubject
.To = xRgSendVal
.HTMLBody = xMailBody
.Display
'.Send
End With
Set xMailItem = Nothing
ElseIf CDate(xRgDateVal) - Date <= 0 And CDate(xRgDateVal) - Date > -1460 Then
xRgSendVal = xRgSend.Offset(i - 1).Value
xRgNameVal = xRgName.Offset(i - 1).Value
xMailSubject = xRgText.Offset(i - 1).Value & " on " & xRgDateVal
vbCrLf = "<br><br>"
xMailBody = "<HTML><BODY>"
xMailBody = xMailBody & "Dear " & xRgNameVal & vbCrLf
xMailBody = xMailBody & xRgText.Offset(i - 1).Value & vbCrLf
xMailBody = xMailBody & "</BODY></HTML>"
Set xMailItem = xOutApp.CreateItem(0)
With xMailItem
.Subject = xMailSubject
.To = xRgSendVal
.HTMLBody = xMailBody
.Display
'.Send
End With
Set xMailItem = Nothing
End If
Next
Set xOutApp = Nothing
End Sub
该代码仅包含一个next和for语句,因此我假设这不是实际的错误,它是由导致编译器无法到达“ next”语句的语句中的错误所生成的。因此,我删除了“ For”和“ Next”语句之外的部分,以帮助重点解决该问题。如果需要,可以提供其余代码。