我有一个用 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!
非常感谢!
最佳答案
你可以只用一个带有条件连接的 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"
关于VBA - 如果单元格为空白,则忽略 strBody 的一部分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35294305/