迭代VBA中的列(Excel)

时间:2010-07-29 05:25:46

标签: excel vba loops

当我输入该标题​​时,我遇到了许多帮助主题,并查找了其中一些并获得了一些基本想法。我仍然感到困惑,这就是创建新帖子以获得一些指导的原因。

情景是这样的:

我的Excel工作表在A1中有Y或N字母,B1中有一些文字,C1中有效的未来日期。

我目前有一个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 = "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. 检查所有单元格(i,3),其中i是1到最后一个非空行。
  2. 操作日期 - 单元格(i,3)中每个数据的15天。
  3. 如果其中任何一个与今天匹配,则检查它的相应(i,1)列。
  4. 如果该特定(i,1)设置为N,则触发电子邮件。
  5. 我有一个mSend标志。它最初将为0.如果第四个条件满足,那么我将其设置为1,然后在触发电子邮件之前检查mSend值。

    有人请指导。

1 个答案:

答案 0 :(得分:3)

我自己也明白了:)

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

谢谢!