我在A列中列出了接收样品的日期,在I列中列出了处理样品的时间。我希望Excel每周发送一次电子邮件,直到第一栏被填满为止。
我尝试做一个“如果那么”的声明,但它不起作用。
第一句话应该是
“如果A列不为空,I列为空,则每周发送电子邮件”
“如果两列都不为空,则无需发送电子邮件。”
我不一定需要消息框,但是如果没有任何内容,我无法结束程序。
我试图制作代码主体,以便稍后查找自动化。
Dim rng1 As Range
Dim rng2 As Range
Set rng1 = Range("A:A")
Set rng2 = Range("I:I")
For Each Value In rng1
If rng1.Text <> "" And rng2.Text = "" Then
Dim aOutlook As Object
Dim aEmail As Object
Dim rngeAddresses As Range, rngeCell As Range, strRecipients As String
Set aOutlook = CreateObject("Outlook.Application")
Set aEmail = aOutlook.CreateItem(0)
aEmail.Importance = 2
aEmail.Subject = "You have a message from Quarantine"
aEmail.Body = "Please complete the excel sheet for samples"
aEmail.Recipients.Add "me@email.com"
aEmail.Send
ElseIf rng1.Text <> "" And rng2.Text <> "" Then MsgBox "All projects are up to date"
ElseIf rng1.Text = "" Then
End If
Next
End Sub
答案 0 :(得分:0)
在使用For Each
循环时,需要确保您引用的是您要迭代的变量-在您的情况下,该变量是Value
,但您忘记这样做了。为了您的代码,我将Value
替换为myval
-我不建议使用Value
作为变量名,因为它在语法中使用了很多,所以您要最终使自己困惑。
不需要rng2
-只需使用.Offset
来检查I列中的值。
您还需要一种在行数用尽时退出循环的方法-否则,您会无缘无故地遍历一百万行。
我还删除了您未使用的声明,并将其他外部声明移到了子例程的顶部。声明一次并完成。
Option Explicit
Sub Test()
Dim rng1 As Range, myval As Range, rngeAddresses As Range
Dim aOutlook As Object, aEmail As Object
Set rng1 = Range("A:A")
For Each myval In rng1
'Check column A and column I
If myval.Text <> "" And myval.Offset(0, 8).Text = "" Then
Set aOutlook = CreateObject("Outlook.Application")
Set aEmail = aOutlook.CreateItem(0)
aEmail.Importance = 2
aEmail.Subject = "You have a message from Quarantine"
aEmail.Body = "Please complete the excel sheet for samples"
aEmail.Recipients.Add "me@email.com"
'aEmail.Send
ElseIf myval.Text = "" Then
Exit For
End If
Next
End Sub