【问题标题】:VBA - Ignore part of strBody if cell is blank?VBA - 如果单元格为空白,则忽略 strBody 的一部分?
【发布时间】:2016-02-09 22:27:28
【问题描述】:

我有一个在 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!

非常感谢!

【问题讨论】:

    标签: vba excel email


    【解决方案1】:

    你可以只用一个带有条件连接的 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"
    

    【讨论】:

    • 另一个可行的解决方案 :) 很有启发性,谢谢!
    【解决方案2】:

    我会有一个 if 语句来查看它是否为空,然后跳过它。因此,不要将一行代码中的所有 & 分解成不同的句子。检查字符串中是否有任何值,然后将它们附加到一个字符串中以进行发送。 它将添加更多代码,但看起来会更好。 因此,例如,如果您有 B 部分,那么您的消息将附加到发送字符串 = A+B+C 但是,如果您没有 B 的值,那么您的 if 语句将跳过 b 部分并仅将 a 和 c 附加到您的发送字符串。
    我认为这样的事情对你的目的有用。

    【讨论】:

    • 正是我的想法 - 只是我提供了代码!
    • 我在发布后就看到了。英雄所见略同。 :)
    【解决方案3】:

    添加一个 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。

    【讨论】:

      猜你喜欢
      • 2021-02-05
      • 2015-12-27
      • 1970-01-01
      • 1970-01-01
      • 2018-11-20
      • 2020-02-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多