2010-07-29 178 views
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 

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

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

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

有人请指导。

回答

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 

谢谢!

+0

宾果。你甚至可以通过使用“If A = B and C = D then ...”来摆脱其中一个If循环。 – PowerUser 2010-07-29 14:26:47