我需要一个脚本,该脚本从第三列的顶部到底部查看数字,如果看到0和大于100的数字,则该数字的值将在电子邮件标题中发送。 当前版本的编码将所有值发送到电子邮件(条件不起作用)。 但是,如果将GoTo替换为Msgbox,则脚本可以完美运行...
For i = 2 To 100 Step 1
If cells(i, 3) = 0 And cells(i - 1, 3) < 100 Then Exit For
If cells(i, 3) = 0 And cells(i - 1, 3) > 100 Then GoTo email
If cells(i, 3) = 0 And cells(i - 1, 3) > 100 Then Exit For
Next i
email:
Dim olObj_1 As Outlook.Application
Dim mItem_1 As Outlook.MailItem
Set olObj_1 = New Outlook.Application
Set mItem_1 = olObj_1.CreateItem(olMailItem)
With mItem_1
.To = "xxxx@xxxx.com"
.Subject = "Figure_one " & cells(i - 1, 3)
.Send
End With
End Sub
答案 0 :(得分:0)
尝试一下:
Option Explicit
Sub Mail()
Dim i As Long
For i = 2 To 100
If Cells(i, 3) = 0 And Cells(i - 1, 3) > 100 Then
Dim olObj_1 As Outlook.Application
Dim mItem_1 As Outlook.MailItem
Set olObj_1 = New Outlook.Application
Set mItem_1 = olObj_1.CreateItem(olMailItem)
With mItem_1
.To = "xxxx@xxxx.com"
.Subject = "Figure_one " & Cells(i - 1, 3)
.Send
End With
Exit For
End If
Next i
End Sub
我假设您只想发送1封电子邮件,否则您的代码应该全部更改。
答案 1 :(得分:0)
我认为您可以尝试以下方法:
Option Explicit
Sub test()
Dim olObj_1 As Outlook.Application
Dim mItem_1 As Outlook.MailItem
Dim str As String
Dim i As Long
With ThisWorkbook.Worksheets("Sheet1") ' Always select your worksheet name
For i = 2 To 100
If .Cells(i, 3).Value = 0 And .Cells(i - 1, 3).Value > 100 Then
If str = "" Then
str = "Figure_one " & .Cells(i - 1, 3).Value
Else
str = str & ", Figure_one " & .Cells(i - 1, 3).Value
End If
End With
Next i
Set olObj_1 = New Outlook.Application
Set mItem_1 = olObj_1.CreateItem(olMailItem)
With mItem_1
.To = "xxxx@xxxx.com"
.Subject = str
.Send
End With
End With
End Sub