我在Excel 2010中创建了一个VBA脚本,它在任务到达截止日期前30分钟向我发送电子邮件。消息“strBody”的主体获取三个单元格的值以使消息唯一。它获取任务的名称,任务的注释和任务的截止日期。
我想知道的是,如果其中一个引用的单元格为空,是否有一种方法可以省略此消息的一部分。
在下面的模板中:
"This email is to notify you that your task : " & Cells(FormulaCell.Row, "A").Value & " with the following note: " & Cells(FormulaCell.Row, "B").Value & " is nearing its Due Date: " & Cells(FormulaCell.Row, "E").Value & "." & vbNewLine & "A wise decision would be to complete this task before it expires!" & _
vbNewLine & vbNewLine & "Truly yours," & vbNewLine & "Task Manager"
如果Cells(FormulaCell.Row,“B”)为空白,我希望删除以下部分。
" with the following note: " & Cells(FormulaCell.Row, "B").Value & "
这主要是一个清理过程,因为消息中的空白区域非常漂亮,我将其用作CC中多人的业务工具。
目前,带有空白注释区域的消息如下所示:
Hello,
This email is to notify you that your task : Give Denise a Promotion, with the following note: is nearing its due date: 02/09/2016.
A wise decision would be to complete the task before it expires!
非常感谢!
答案 0 :(得分:4)
只能使用带条件连接的一个If语句
msg = "This email is to notify you that your task : " & Cells(FormulaCell.Row, "A").Value
If Cells(FormulaCell.Row, "B").Value <> "" Then
msg = msg & " with the following note: " & Cells(FormulaCell.Row, "B").Value
end if
msg = msg & Cells(FormulaCell.Row, "A").Value & " is nearing its Due Date: " & Cells(FormulaCell.Row, "E").Value & "." & vbNewLine & "A wise decision would be to complete this task before it expires!" & vbNewLine & vbNewLine & "Truly yours," & vbNewLine & "Task Manager"
答案 1 :(得分:3)
我会有一个if语句来查看它是否为空,然后只是跳过它。因此,不是使用所有&amp;的一行代码,而是将其分解为不同的句子。检查字符串中是否有任何值,然后将它们附加到一个字符串中进行发送。
它会添加更多代码,但看起来会更好。
因此,例如,如果您有一个B部分,那么您的消息将附加到发送sting = A + B + C.
但是如果你没有B的值,那么你的if语句将跳过b部分,只将a和c附加到你的发送字符串。
我想这样的东西可以用于你的目的。
答案 2 :(得分:1)
添加IF ELSE声明。
If Cells(FormulaCell.Row, "B").Value = "" Then
msg = "This email is to notify you that your task : " & Cells(FormulaCell.Row, "A").Value & " is nearing its Due Date: " & Cells(FormulaCell.Row, "E").Value & "." & vbNewLine & "A wise decision would be to complete this task before it expires!" & _
vbNewLine & vbNewLine & "Truly yours," & vbNewLine & "Task Manager"
Else
msg = "This email is to notify you that your task : " & Cells(FormulaCell.Row, "A").Value & " with the following note: " & Cells(FormulaCell.Row, "B").Value & " is nearing its Due Date: " & Cells(FormulaCell.Row, "E").Value & "." & vbNewLine & "A wise decision would be to complete this task before it expires!" & _
vbNewLine & vbNewLine & "Truly yours," & vbNewLine & "Task Manager"
End If
如果除note之外的其他字段也可能为空白,则在其中添加ElseIf。