如果单元格为空,如何在VBA中通过电子邮件发送省略了单元格/变量的表

时间:2019-03-21 16:45:16

标签: excel vba

我正在尝试找出如何省略表格中的空白单元格以发送电子邮件。该表是按月的一天,它衡量我们的员工绩效。如果通过电子邮件发送该单元格中没有任何值,则我想完全省略该列,因此该表的长度不超过30或31列。例如,某员工每月工作14天,因此我想要一个表来显示绩效报告的那14天。

我有一个通用宏,用于通过表格发送大量电子邮件,但这是我以前从未做过的事情,并且不确定在脚本中的哪个位置或什至从哪里开始。

我也有一个问题,关于如果在手机上检查电子邮件,表格是否正确显示。该表显示了,但没有像我在Outlook上检查电子邮件时那样的边框。这不是一个重要的优先事项,也许应该在一个单独的问题中提出。

这就是我正在使用的表的样子:

https://imgur.com/a/0z9lLRO

Sub SendEmail(what_address As String, subject_line As String, mail_body As String)    
    Dim olApp As Outlook.Application
    Set olApp = CreateObject("Outlook.Application")

    Dim olMail As Outlook.MailItem
    Set olMail = olApp.CreateItem(olMailItem)

    olMail.To = what_address
    olMail.Subject = subject_line
    olMail.HTMLbody = mail_body
    olMail.Send 
End Sub


Sub SendPerformance()

    row_number = 3

    Do
        DoEvents
        row_number = row_number + 1
        Dim mail_body_message As String
        Dim full_name As String
        Dim replace_daily_average As String
        Dim replace_percentage As String
        Dim replace_comparison As String
        Dim StrBody As String

        full_name = ActiveSheet.Range("A" & row_number)
        percent_average = ActiveSheet.Range("AG" & row_number)
        comp_difference = ActiveSheet.Range("AH" & row_number)
        daily_average = ActiveSheet.Range("B" & row_number)

        mail_body_message = Replace(mail_body_message, "replace_name_here", full_name)
        mail_body_message = Replace(mail_body_message, "replace_daily_average", daily_average)
        mail_body_message = Replace(mail_body_message, "replace_percentage", percent_average)
        mail_body_message = Replace(mail_body_message, "replace_comparison", comp_difference)


        StrBody = "<html> <head> <style> br, table, table style {width: 300px;height: 315px;border: 1px solid black}, th {bpadding: 1px; border: 1px solid black;alignment: center}, td {bpadding: 3px; border: 1px solid black;alignment: center} </style> <head> <body> <table>"
        mail_body_message = "<span style=" & """" & "font-weight: bold;" & "color: #0000ff;" & """" & ActiveSheet.Range("AK4") & ">" & ActiveSheet.Range("AK4") & "<br/><br/>" & "<span style=" & """" & "font-weight: bold;" & "color: #ff0000;" & """" & ActiveSheet.Range("AK5") & ">" & ActiveSheet.Range("AK5") & "<br/><br/>" & "<span style=" & """" & "font-weight: bold;" & "color: #ff0000;" & """" & ActiveSheet.Range("AK6") & ">" & ActiveSheet.Range("AK6") & "<br/><br/>" & StrBody & vbNewLine & _
            "<tr>" & _
                "<th>" & ActiveSheet.Range("B2") & "</th>" & _
                "<th>" & ActiveSheet.Range("B3") & "</th>" & _
                "<td>" & daily_average & "</td></tr>" & _
                "</table>"

        mail_body_message = Replace(mail_body_message, "replace_name_here", full_name)
        mail_body_message = Replace(mail_body_message, "replace_daily_average", daily_average)
        mail_body_message = Replace(mail_body_message, "replace_percentage", percent_average)
        mail_body_message = Replace(mail_body_message, "replace_comparison", comp_difference)
        Call SendEmail(ActiveSheet.Range("AI" & row_number), ActiveSheet.Range("AK3"), mail_body_message)
    Loop Until row_number = 50
End Sub

我希望它看起来像什么:https://imgur.com/a/mRimK6j

它看起来像X月份中某天的电子表格。每一行都是不同的员工,并且该单元格中只有工作日的信息。因此,我使用的代码已通过电子邮件发送给员工日程安排。我通过将表复制并粘贴到Outlook中,然后删除空白列将其链接起来。另外,该代码也被简化为表,因为我不知道如何使表仅显示其中具有值的列和行。 imgur.com/a/0z9lLRO-这就是电子表格的外观。我要省略的单元格是其中没有值的单元格。

只要有意义,表格可以是垂直或水平的。 请让我知道是否还有其他信息可以帮助您!

0 个答案:

没有答案