gpt4 book ai didi

vba - 代码不检查空字符串

转载 作者:行者123 更新时间:2023-12-01 11:38:09 25 4
gpt4 key购买 nike

我不确定为什么我的代码没有输出我的消息,即使我的电子表格有一个空字段。如果我在消息框显示它为空的值,所以不确定为什么它没有在 IsEmpty 函数中拾取它。

Sub Button1_Click()

Dim Cell As Range
Dim name As Range
Dim objDate As Date
Dim myName As String


For Each Cell In Range("P3:P4").Cells

myName = Cell.Offset(0, -14).Value

If Not IsDate(Cell.Value) Then

MsgBox "Please make sure all dates are in a valid form: DD.MM.YYYY"

ElseIf Cell.Value <= Date + 30 Then

**ElseIf IsEmpty(myName) Then

MsgBox "Please enter a name"**

Else

Dim appOutlook As Outlook.Application
Dim mitOutlookMsg As Outlook.MailItem
Dim recOutlookRecip As Outlook.Recipient

' Step 1: Initialize an Outlook session.
Set appOutlook = CreateObject("Outlook.Application")
' Step 2: Create a new message.
Set mitOutlookMsg = appOutlook.CreateItem(olMailItem)
With mitOutlookMsg
' Step3: Add the To recipient(s) to message.
Set recOutlookRecip = .Recipients.Add(myName)
recOutlookRecip.Type = olTo
'Set valid properties like Subject, Body, and Importance of the message.
.Subject = "Test123"
'.Body = "Test"
.BodyFormat = olFormatHTML
.HTMLBody = "Dear " & myName & " TEST EMAIL "
.Importance = olImportanceHigh 'High importance
' Resolve every Recipient's name
For Each recOutlookRecip In .Recipients
recOutlookRecip.Resolve
If Not recOutlookRecip.Resolve Then
mitOutlookMsg.Display
End If
Next
.Send
End With
Set mitOutlookMsg = Nothing
Set appOutlook = Nothing

MsgBox "Emails have been sent"

End If
Next Cell


End Sub

最佳答案

Cell.Value <= Date + 30将始终返回 TRUE当单元格为空时。

移动ElseIf IsEmpty(myName) Then之前 ElseIf Cell.Value <= Date + 30 Then

例如

If IsEmpty(myName) Then
MsgBox "Please enter a name"
ElseIf Not IsDate(Cell.Value) Then
MsgBox "Please make sure all dates are in a valid form: DD.MM.YYYY"
ElseIf Cell.Value <= Date + 30 Then

Else

编辑:以下部分回答了您在评论中提出的问题。

如果您不想在一个单元格为空时运行代码,那么您可以使用此代码。这将检查是否 count of cells = number of cells filled .如果它们不相同,则表示一个或多个单元格为空。

Sub Sample()
Dim rng As Range

Set rng = Range("P3:P4").Cells

If rng.Count <> Application.WorksheetFunction.CountA(rng) Then
MsgBox "Please ensure that all cells are filled with dates in the range"
Exit Sub
End If

'
'~~> Rest of the code
'
End Sub

关于vba - 代码不检查空字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25241085/

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