【问题标题】:How to run code on column data every 9th row?如何每 9 行在列数据上运行代码?
【发布时间】:2019-09-17 21:37:52
【问题描述】:

我已经按照我想要的方式工作了。但是,它似乎在 K13:K5000 的 K 列中的所有行上运行。

如何让这段代码只在 K13 之后的第 9 行运行一次?

Dim xRg As Range

Private Sub Worksheet_Change(ByVal Target As Range)
    On Error Resume Next
    If Target.Cells.Count > 1 Then Exit Sub
    If Not Intersect(Target, Range("K13:K5000")) Is Nothing Then
        If IsDate(Target.Value) And Target.Value > 0 Then
            targetRow = Target.Row
            offsetRow = Target.Offset(9, 0).Row
            Call Mail_small_Text_Outlook(targetRow, offsetRow)
        End If
    End If
End Sub

Sub Mail_small_Text_Outlook(targetRow, offsetRow)
    Dim xOutApp As Object
    Dim xOutMail As Object
    Dim xMailBody As String
    Set xOutApp = CreateObject("Outlook.Application")
    Set xOutMail = xOutApp.CreateItem(0)
    xMailBody = "Hello" & vbNewLine & vbNewLine & _
              "This client is now Committed & Complete and ready for your attention" & vbNewLine & vbNewLine & _
              "Renew As Is?" & vbNewLine & _
              "Adding Changing Groups?"
              
    On Error Resume Next
    With xOutMail
        .To = ""
        .CC = ""
        .BCC = ""
        .Subject = "Committed & Complete"
        .Body = xMailBody
        .Display   
    End With
    On Error GoTo 0
    Set xOutMail = Nothing
    Set xOutApp = Nothing
End Sub

【问题讨论】:

  • 使用 For 循环,第 9 步。

标签: excel vba outlook


【解决方案1】:

targetRow = Target.Row 之后运行检查以查看它是否是第 9 行。这可以使用 Mod 函数来执行,将 targetRow 的余数除以 9(当然是在减去 13 之后,因为您从第 13 行开始)。类似的东西

Dim bIsNinthRow as Boolean
Dim ModResult as Long
bIsNinthRow = False
ModResult = (targetRow - 13) Mod 9
If ModResult = 0 Then bIsNinthRow = True
If bIsNinthRow Then Call Mail_small_Text_Outlook(targetRow, offsetRow)

【讨论】:

    猜你喜欢
    • 2011-06-02
    • 2013-07-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-27
    相关资源
    最近更新 更多