2010-08-12 143 views
0

我有一個if語句如下:IF語句在VBA

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

什麼這個if語句的作用是創建3封電子郵件,從週六和週五,附件和數據鏈接發送,如果在輸入的日期工作表是星期天。如果星期日是2010年8月1日(2010年7月31日)和週五(2010年7月30日)的第一個月,If語句不會識別月份變化,因此無法創建3帶有必要附件和鏈接的電子郵件。

如何創建/更改我的If語句,以便在新月的第一個月落在星期日或星期一時,我必須從上個月提取數據時,它會識別月份更改並創建正確的附件和鏈接?

下面是完整的代碼,我有創建電子郵件...

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 

回答

0

它根據週日的一天是1還是在正確的mysundate您的IF語句之前只需要添加一個檢查星期日後的第一天是1.

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 
+0

@ Thomas,謝謝你的幫忙。但文件的延伸是什麼最終搞亂了我。 「I7」是我的工作簿中有「8月」的單元格。所以我的文件分機正在閱讀\\ firework \ public \ FINANCE \ Daily Report \ FY10 \ Key Indicator \ AUG10 \ Key Indicator Daily Report - 07 \ 30 \ 2010。當AUG10實際上應該是JUL10時,隨着日期的變化。 – Edmond 2010-08-12 19:43:58