在VBA中的IF声明

时间:2010-06-09 15:31:00

标签: excel-vba vba excel

Private Sub sendemail(esubj)

    Sheets("Actual").Select
    myfridate = Cells(1, 3).Value
    myfridate = DateAdd("d", -2, myfdate)
    myfridate = Format(myfridate, "mm-dd-yy")

    Sheets("Actual").Select
    mysatdate = Cells(1, 3).Value
    mysatdate = DateAdd("d", -1, myfdate)
    mysatdate = Format(mysatdate, "mm-dd-yy")


If Weekday(Now()) = vbMonday Then

    Set omail = CreateItem(olMailItem)

    ROW_BEGIN = 1
    ROW_END = 72

    Sheet1.Activate
    Range("I7").Select
    fileSat = "\\FINANCE\Daily Report\"
    fileSat = fileSat & Left(Range("I7"), 3) & Right(Year(Date), 2)
    fileSat = fileSat & "\Key Report - " & mysatdate & ".xls"

    Sheet1.Activate
    Range("I7").Select
    fileSun = "\\FINANCE\Daily Report\"
    fileSun = fileSun & Left(Range("I7"), 3) & Right(Year(Date), 2)
    fileSun = fileSun & "\Key Report - " & mysundate & ".xls"

    Sheet1.Activate
    Range("I7").Select
    fileFri = "\\FINANCE\Daily Report\"
    fileFri = fileFri & Left(Range("I7"), 3) & Right(Year(Date), 2)
    fileFri = fileFri & "\Key Report - " & myfridate & ".xls"


    With omail

    .Subject = "M Daily Report"
    .BodyFormat = olFormatHTML
    .HTMLBody = "<a href ='" & fileFri & "'>Key Report - " & myfridate & "</a><br><a href ='" & fileSat & "'>Key Indicator Daily Report - " & mysatdate & "</a><br><a href ='" & fileSun & "'>Key Indicator Daily Report - " & mysundate & "</a>"
    .To = "Me"
    .Display

    End With

    Set omail1 = CreateItem(olMailItem)

    With omail1

    .Subject = "R Daily Report"
    .BodyFormat = olFormatHTML
    .To = "You"
    .Attachments.Add fileFri
    .Attachments.Add fileSat
    .Attachments.Add fileSun
    .Display

    End With

    Set omail2 = CreateItem(olMailItem)

    With omail2

    .Subject = "Mc Daily Report"
    .BodyFormat = olFormatHTML
    .To = "them"
    .Attachments.Add fileFri
    .Attachments.Add fileSat
    .Attachments.Add fileSun
    .Display

End With

Else

    ROW_BEGIN = 1
    ROW_END = 72

    Sheet1.Activate
    Range("I7").Select
    fileSun = "\\FINANCE\Key Indicator\"
    fileSun = fileSun & Left(Range("I7"), 3) & Right(Year(Date), 2)
    fileSun = fileSun & "\Key Report - " & mysundate & ".xls"


    Set omail = CreateItem(olMailItem)

    With omail

    .Subject = "M Daily Report"
    .BodyFormat = olFormatHTML
    .HTMLBody = "<a href ='" & fileSun & "'>Key Report - " & mysundate & "</a>"
    .To = "Me"
    .Display

    End With

    Set omail1 = CreateItem(olMailItem)

    With omail1

    .Subject = "R Daily Report"
    .BodyFormat = olFormatHTML
    .To = "You"
    .Attachments.Add fileSun
    .Display

    End With

    Set omail2 = CreateItem(olMailItem)

    With omail2

    .Subject = "Mc Daily Report"
    .BodyFormat = olFormatHTML
    .To = "them"
    .Attachments.Add fileSun
    .Display

End With

End If

'ActiveWorkbook.Close
Set omail = Nothing

End Sub

我在vba中有代码,如果工作日是星期一,excel将生成3封带有3个附件/链接的电子邮件。但如果不是星期一,excel将生成3封电子邮件,只有1个附件/链接。我的问题是,在我的Excel电子表格中有一个名为Actual的选项卡,它填充了一个日期。如果我的Excel电子表格中的这个日期在星期一,一周中的任何其他日期都被更改,我的vba代码仍将该程序视为星期一。我需要一个IF语句,它允许3封带有3个附件/链接的电子邮件生成,并在我的电子表格中的“实际”选项卡上输入日期。我希望这不会令人困惑。

2 个答案:

答案 0 :(得分:1)

目前,评估等式为星期一(If Weekday(Now()) = vbMonday Then )的代码行正在检查当前日期,而不是名为“Actual”的工作表上的日期。您应该将该行更改为:

If Weekday(Worksheets("Actual").Range("A1").Value) = vbMonday Then

您需要将A1替换为您要检查的日期值的位置,但如果格式正确,则应返回日期。

答案 1 :(得分:1)

要为星期五或星期六指定不同的操作,您可以使用Elseif添加第二个语句。

If Weekday(Worksheets("Actual").Range("A1").Value) = vbMonday Then
    'Do this
Elseif Weekday(Worksheets("Actual").Range("A1").Value) = vbFriday or _
Weekday(Worksheets("Actual").Range("A1").Value) = vbSaturday Then
    'Do this instead
Else
    'What happens for all other days
End If

如果星期五或星期六的日子可以像“退出子”一样简单,那么“请改为”,这意味着没有任何反应。请注意,“或”语句要求您重新声明所有条件。伪代码:“如果今天是星期五或星期六”将不起作用,你必须使用“如果今天是星期五或今天是星期六”。

如果您发现自己的需求更加具体,可以为一周中的每一天设置一个切换声明。