1
當我輸入該標題時,遇到了許多幫助主題,並查找了其中的一些主題,並得到了一些基本的想法。我仍然陷入困境,這就是爲什麼創建一個新的職位,以獲得一些指導。在VBA中對列進行迭代(Excel)
方案是這樣的:
我的excel工作表在A1,B1中一些文本,在C1的有效未來日期Y或N信。
我現在有一個VBA腳本,做這行單行:
檢查(日期C1日 - 15日)是否是今天的日期,然後檢查A1是N.如果這兩個條件都爲真然後發送一封自動電子郵件。
我不知道的是,我如何使它的功能爲n行?
這是我現在所擁有的。
Sub SendAlert()
'Set up the objects required for Automation into lotus notes
Dim Maildb As Object 'The mail database
Dim UserName As String 'The current users notes name
Dim MailDbName As String 'The current users notes mail database name
Dim MailDoc As Object 'The mail document itself
Dim AttachME As Object 'The attachment richtextfile object
Dim Session As Object 'The notes session
Dim EmbedObj As Object 'The embedded object (Attachment)
Dim stSignature As String
Dim i As Integer
Dim lastRow As Long
'Start a session to notes
Set Session = CreateObject("Notes.NotesSession")
'Get the sessions username and then calculate the mail file name
'You may or may not need this as for MailDBname with some systems you
'can pass an empty string
UserName = Session.UserName
MailDbName = Left$(UserName, 1) & Right$(UserName, (Len(UserName) - InStr(1, UserName, " "))) & ".nsf"
'Open the mail database in notes
Set Maildb = Session.GETDATABASE("", MailDbName)
If Maildb.IsOpen = True Then
'Already open for mail
Else
Maildb.OPENMAIL
End If
'Set up the new mail document
Set MailDoc = Maildb.CREATEDOCUMENT
MailDoc.Form = "Memo"
'Setting Flag to decide whether to send the mail or not
mSend = 0
'Copying Cells - Cell A1 as Copying Status , Cell B1 as Pending Tasks Cell C1 as Deadline Date
cStatus = cStatus & Cells(1, 1)
Msg = Msg & Cells(1, 2)
dDate = dDate & Cells(1, 3)
**'Looping through the cells**
lastRow = ActiveSheet.UsedRange.SpecialCells(xlCellTypeLastCell).Row 'Gets the last row number
For i = 1 To lastRow
if & Cells(i,1) = "N"
'Attach Your Signature
stSignature = "Regards," & vbCrLf & "Some Name"
'MailDoc.Recipient = Recipient
Addressee = "[email protected],"
Recipient = Split(Addressee, ",")
MailDoc.sendto = Recipient
'MailDoc.Subject = Subject
MailDoc.Subject = "Pending Activities Report"
'MailDoc.Body = BodyText
MailDoc.Body = "Dear NXC Team," & vbCrLf & vbCrLf & "Here are the pending activities:" & vbCrLf & vbCrLf & Msg & vbCrLf & vbCrLf & stSignature
'MailDoc.SAVEMESSAGEONSEND = SaveIt
MailDoc.SAVEMESSAGEONSEND = True
'Send the document
If (DateAdd("d", -15, dDate) = Date) And (cStatus = "N") Then
MailDoc.SEND 0, Recipient
End If
'Clean Up
Set Maildb = Nothing
Set MailDoc = Nothing
Set AttachME = Nothing
Set Session = Nothing
Set EmbedObj = Nothing
End Sub
循環通過細胞是我無法理解的。我甚至有邏輯..
- 檢查所有細胞(我,3),其中我是從1到最後一個非空行。
- 操作日期 - 單元格(i,3)中的每個數據都需要15天。
- 如果其中任何一個與今天匹配,然後檢查它是相應的(i,1)列。
- 如果該特定(i,1)設置爲N,則觸發電子郵件。
我有一個mSend標誌。它最初將爲0.如果第四個條件滿足,那麼我將它設置爲1,然後在觸發電子郵件之前檢查mSend值。
有人請指導。
賓果。你甚至可以通過使用「If A = B and C = D then ...」來擺脫其中一個If循環。 – PowerUser 2010-07-29 14:26:47