我有一个带有HTML正文的模板。我想根据单元格F的值包含一行。
如果在单元格F中写入“不完整”,则应在电子邮件文本中包含短语“您的付款不完整”。
如果在单元格F中写入“完成”,则会向HTML正文添加一个空格。
我怎样才能做到这一点?
With OutMail
.To = Cells(cell.Row, "C").Value
.Subject = "Request" & Cells(cell.Row, "E").Value
.BodyFormat = olFormatHTML
.HTMLBody = Cells(cell.Row, "A").Value & Cells(cell.Row, "B").Value _
& "<p>The status of your invoice is</p>" _
If (Cells(cell.Row, "F").Value) = "incomplete" then:
.HTMLBody = "<p> Your payment is incomplete<p/>" _
Else
.HTMLBody = "<p> <p/>" _
& "<p>Thank you</p>" _
如果循环看起来像这样的Vba。
If condition
'if true
Else
'if false
EndIf
你的代码中有一些错误,有些东西会造成混乱。首先是if语句。写这样的,而不是:
If (Cells(cell.Row, "F").Value) = "incomplete" Then
.HTMLBody = "<p> Your payment is incomplete<p/>"
Else
.HTMLBody = "<p> <p/>" & _
"<p>Thank you</p>"
End If
其次,您的单元格引用不正确。列在单元格方法中由数字引用(即F = 6)。期望活动纸张是预期纸张也是有风险的。创建对它的引用以确保
Dim TargetSheet as WorkSheet
Set TargetSheet = Worksheets("MySheet")
TargetSheet.Cells(cell.Row, 6).Value
使用IIF()功能
With OutMail
.To = Cells(cell.Row, "C").value
.Subject = "Request" & Cells(cell.Row, "E").value
.BodyFormat = olFormatHTML
.HTMLBody = Cells(cell.Row, "A").value & Cells(cell.Row, "B").value _
& "<p>The status of your invoice is</p>" _
& IIf(Cells(cell.Row, "F").value = "incomplete", "<p> Your payment is incomplete<p/>", "<p> <p/><p>Thank you</p>")
End With
只需根据需要调整函数的两个结果:就像现在一样,它会添加"<p> Your payment is incomplete<p/>"
或"<p> <p/><p>Thank you</p>"
,与Cells(cell.Row, "F").value
matching“不完整”有关