【问题标题】:Iterating through columns in VBA (Excel)遍历 VBA (Excel) 中的列
【发布时间】:2010-07-29 05:25:45
【问题描述】:

当我输入该标题​​时,我遇到了许多帮助主题,并查找了其中的几个并获得了一些基本概念。我仍然陷入困境,这就是为什么要创建一个新帖子来获得一些指导。

场景是这样的:

我的 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 从 1 到最后一个非空白行的所有单元格 (i,3)。
  2. 为单元格 (i,3) 中的每个数据处理日期 - 15 天。
  3. 如果其中任何一个与今天匹配,请检查其对应的 (i,1) 列。
  4. 如果特定的 (i,1) 设置为 N,则触发电子邮件。

我有一个 mSend 标志。初始值为 0。如果满足第 4 个条件,则将其设置为 1,然后在触发电子邮件之前检查 mSend 值。

请有人指导。

【问题讨论】:

    标签: excel vba loops


    【解决方案1】:

    我自己明白了:)

    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
    

    谢谢!

    【讨论】:

    • 宾果游戏。您甚至可以使用“If A=B and C=D then...”摆脱其中一个 If 循环
    猜你喜欢
    • 2016-07-10
    • 1970-01-01
    • 2019-10-21
    • 2010-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-14
    • 1970-01-01
    相关资源
    最近更新 更多