VBA - 如果单元格为空白,则忽略 strBody 的一部分?

标签 vba excel email

我有一个用 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/

相关文章:

excel - 使用Excel作为Access数据库的前端(使用VBA)

Python:不断检查新电子邮件并提醒更多新电子邮件

excel - VBA - 将 Sheet1 值与 Sheet2 进行比较,将结果复制/粘贴到 Sheet3

vba - Excel VBA范围变量连接

excel - laravel::在一个表中插入数组的某些值,在另一个表中插入其他值

email - 无法使用 gmail 对 SMTP 服务器错误进行身份验证

email - 向 PayPal 买家发送电子邮件

excel - 在另一个工作表中记录 'RTD' 值更改

vba - 事件 IE11 发生变化

Javascript IRR(内部 yield )公式准确性