迭代VBA中的列(Excel)

当我input那个标题时,我遇到了许多帮助主题,并查阅了其中的一些,并得到了一些基本的想法。 我仍然陷入困境,这就是为什么创build一个新的职位,以获得一些指导。

情景是这样的:

我的excel表单在A1中有Y或N个字母,在B1中有一些文本,在C1中有一个有效的未来date。

我目前有一个VBA脚本,这样做的单行:

检查(C1 -15天的date)是否为今天的date,然后检查A1是否为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 = "someemailaddress@yahoo.com," 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 

通过细胞循环是我无法理解的。 我甚至有逻辑

  1. 检查所有的细胞(我,3),我是从1到最后一个非空行。
  2. 操作date – 单元格(i,3)中的每个数据均为15天。
  3. 如果其中任何一个与今天匹配,然后检查它是相应的(i,1)列。
  4. 如果该特定的(i,1)被设置为N,则触发电子邮件。

我有一个mSend标志。 它最初将是0.如果第四个条件满足,那么我将它设置为1,然后检查mSend值,然后触发电子邮件。

有人请指导。

我自己得到了:)

 lastRow = ActiveSheet.UsedRange.SpecialCells(xlCellTypeLastCell).Row 'Gets the last row number For i = 1 To lastRow If DateAdd("d", -15, Cells(i, 3)) = Date Then If Cells(i, 1) = "N" Then mSend = 1 Msg = Msg & Cells(i, 2) End If End If Next i 

谢谢!