gpt4 book ai didi

vba - 如果列是空白excel vba,则停止宏

转载 作者:行者123 更新时间:2023-12-04 22:01:08 25 4
gpt4 key购买 nike

我已经编写了一些代码,但如果 R​​2:34 范围为空白,它仍会打开模板电子邮件,其中没有数据。请告诉我我在哪里做不好的连接。

Sub 1()
Dim OutApp As Object
Dim OutMail As Object
Dim sTo As String
Dim spo As String
Dim emailRng As Range, cl As Range, dtrecuta As Range
Dim c As Range
For Each cell In Cells.Range("N2:N34")
If LCase(Cells(cell.Row, "N").Value) = "0" Or LCase(Cells(cell.Row, "N").Value) < "480" Then
On Error Resume Next
Cells(cell.Row, "R").Value = Cells(cell.Row, "M").Value
Else
Cells(cell.Row, "R").Value = Null
End If
Next cell
a = CLng(Date)
Set emailRng = Worksheets("Sheet1").Range("r2:r34")
Set dtrecuta = Worksheets("Sheet1").Range("P2")
For Each cl In emailRng
sTo = sTo & ";" & cl.Value
Next
sTo = Mid(sTo, 2)
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItemFromTemplate("C:\Users\Marius\AppData\Roaming\Microsoft\Templates\statistica.oft")
On Error Resume Next
With OutMail
.To = sTo
.CC = ""
.BCC = ""
.Subject = "TESTARE Statistica pentru data de " & dtrecuta
strbody = "Buna " & " , " & vbNewLine & vbNewLine & _
"Te rog sa trimiti statistica astazi " & a & " pana in ora 10:00, " & _
" pentru data de " & dtrecuta & vbNewLine & vbNewLine & "O zi buna." & _
" " & vbNewLine & vbNewLine & " Acesta este un mesaj automat nu raspundeti la acest e-mail. "
.Display
.Body = strbody & Signature
.send
End With
On Error GoTo cleanup
Set OutMail = Nothing
cleanup:
Set OutMail = Nothing
Set OutApp = Nothing
Application.ScreenUpdating = True

End Sub

最佳答案

在设置范围后添加一个 IF 语句以检查它是否都是空白单元格:

Set emailRng = Worksheets("Sheet1").Range("r2:r34")
If WorksheetFunction.CountBlank(emailRng) = emailRng.Cells.Count Then Exit Sub 'No data

关于vba - 如果列是空白excel vba,则停止宏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34880797/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com