在VBA中的IF声明

时间:2010-08-12 16:30:42

标签: access-vba

我的If语句如下:

If Weekday(Worksheets("Actual").Range("C1").Value) = vbSunday Then

这个If语句的作用是创建3封带有附件和周六和周五数据链接的电子邮件,如果在工作表中输入的日期是星期日,则发送。如果星期日是一个月的第一天,即(2010年8月1日),在星期六(2010年7月31日)和星期五(2010年7月30日),If语句不识别月份变化,因此无法创建3带有必要附件和链接的电子邮件。

如何创建/更改我的If语句,以便当新月的第1天属于星期日或星期一并且我必须从上个月提取数据时,它将识别月份更改并创建正确的附件和链接??

以下是我创建电子邮件的完整代码......

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(Worksheets("Actual").Range("C1").Value) = vbSunday Then

    Set omail = CreateItem(olMailItem)

    ROW_BEGIN = 1
    ROW_END = 72

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

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

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


    With omail

    .Subject = "M Key Indicator Daily Report"
    .BodyFormat = olFormatHTML
    .HTMLBody = "<a href ='" & fileFri & "'>Key Indicator Daily 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.com"
    .Display

    End With

    Set omail1 = CreateItem(olMailItem)

    With omail1

    .Subject = "R Key Indicator Daily Report"
    .BodyFormat = olFormatHTML
    .To = "you.com"
    .Attachments.Add fileFri
    .Attachments.Add fileSat
    .Attachments.Add fileSun
    .Display

    End With

    Set omail2 = CreateItem(olMailItem)

    With omail2

    .Subject = "K Key Indicator Daily Report"
    .BodyFormat = olFormatHTML
    .To = "them.com"
    .Attachments.Add fileFri
    .Attachments.Add fileSat
    .Attachments.Add fileSun
    .Display

End With

ElseIf Weekday(Worksheets("Actual").Range("C1").Value) = vbFriday Or _
    Weekday(Worksheets("Actual").Range("C1").Value) = vbSaturday Then

Else

    ROW_BEGIN = 1
    ROW_END = 72

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


    Set omail = CreateItem(olMailItem)

    With omail

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

    End With

    Set omail1 = CreateItem(olMailItem)

    With omail1

    .Subject = "R Key Indicator Daily Report"
    .BodyFormat = olFormatHTML
    .To = "you.com"
    .Attachments.Add fileSun
    .Display

    End With

    Set omail2 = CreateItem(olMailItem)

    With omail2

    .Subject = "K Key Indicator Daily Report"
    .BodyFormat = olFormatHTML
    .To = "them.com"
    .Attachments.Add fileSun
    .Display

End With

End If

'ActiveWorkbook.Close
Set omail = Nothing

End Sub

1 个答案:

答案 0 :(得分:0)

只需在您的IF语句之前添加一个检查,根据星期日的日期是1还是星期日的日期为1来确定正确的mysundate

If DatePart("d", mysundate) = 1 _ 
        Or DatePart("d", DateAdd("d",1,mysundate) ) = 1 Then
    mysundate = DateAdd("m",-1,mysundate)
    mysatdate = DateAdd("m",-1,mysatdate)
    ...
End If