Excel VBA在第9行的列中输入日期时创建电子邮件
我是VBA的新手,但希望有人能帮助我。 我已经按我喜欢的方式工作了。但是,对于K13:K5000,它似乎在K列中的所有行上运行。如何使此代码仅在K13之后每第9行运行一次 这是我迄今为止所做的工作Excel VBA在第9行的列中输入日期时创建电子邮件,excel,vba,outlook,Excel,Vba,Outlook,我是VBA的新手,但希望有人能帮助我。 我已经按我喜欢的方式工作了。但是,对于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
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
在
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)
使用
循环,步骤9。