Else if语句不按预期工作。

huangapple go评论66阅读模式
英文:

Else if statement not working as intended

问题

在我的用户表单中,我有两个日期文本框;如果其中一个为空,我希望显示一个错误消息,通知用户在继续之前必须在两个文本框中输入日期。我尝试了以下代码:

If IsEmpty(UserForm1.TextBox2.Value) And Not IsEmpty(UserForm1.TextBox3.Value) Or IsEmpty(UserForm1.TextBox3.Value) And Not IsEmpty(UserForm1.TextBox2.Value) Then
    MsgBox "请填写两个日期字段", vbInformation, "日期范围错误"
End If

在宏的下一步之前,但没有发生任何事情。

Private Sub CommandButton1_Click()
    
If IsEmpty(UserForm1.TextBox2.Value) And Not IsEmpty(UserForm1.TextBox3.Value) Or IsEmpty(UserForm1.TextBox3.Value) And Not IsEmpty(UserForm1.TextBox2.Value) Then
    MsgBox "请填写两个日期字段", vbInformation, "日期范围错误"
End If

If UserForm1.ComboBox1.Value = "(Blank)" Then
    Sheets("FormResults").Range("C5").ClearContents '清除产品类型
Else
    Sheets("FormResults").Range("C5").Value = UserForm1.ComboBox1.Value '替换产品类型
End If
Sheets("FormResults").Range("C4").Value = UserForm1.TextBox1.Value '替换店铺
Sheets("FormResults").Range("C3").Value = UserForm1.TextBox2.Value '替换开始日期
Sheets("FormResults").Range("D3").Value = UserForm1.TextBox3.Value '替换结束日期

Unload Me
End Sub
英文:

In my userfield I have 2 textboxes for dates; if one of them is empty I want an error message to notify the user that they have to enter dates in both textboxes before continuing. I tried:

If IsEmpty(UserForm1.TextBox2.Value) And Not IsEmpty(UserForm1.TextBox3.Value) Or IsEmpty(UserForm1.TextBox3.Value) And Not IsEmpty(UserForm1.TextBox2.Value) Then
    MsgBox "Please fill both date fields", vbInformation, "Date Range Error"
    End If

before my next steps in the macro but nothing happens.

Private Sub CommandButton1_Click()

If IsEmpty(UserForm1.TextBox2.Value) And Not IsEmpty(UserForm1.TextBox3.Value) Or IsEmpty(UserForm1.TextBox3.Value) And Not IsEmpty(UserForm1.TextBox2.Value) Then
MsgBox "Please fill both date fields", vbInformation, "Date Range Error"
End If


If UserForm1.ComboBox1.Value = "(Blank)" Then
Sheets("FormResults").Range("C5").ClearContents 'Clear Product Type
Else
Sheets("FormResults").Range("C5").Value = UserForm1.ComboBox1.Value 'Replace Product Type
End If
Sheets("FormResults").Range("C4").Value = UserForm1.TextBox1.Value 'Replace Store
Sheets("FormResults").Range("C3").Value = UserForm1.TextBox2.Value 'Replace Start Date
Sheets("FormResults").Range("D3").Value = UserForm1.TextBox3.Value 'Replace End Date

Unload Me
End Sub

答案1

得分: 2

如果 Len(UserForm1.TextBox2.Value) = 0 或 Len(UserForm1.TextBox3.Value) = 0 Then
MsgBox "请填写日期字段", vbInformation, "日期范围错误"
End If

此帖所指出,您也可以与 vbNullString 进行比较。

英文:

Simply:

If Len(UserForm1.TextBox2.Value) = 0 Or Len(UserForm1.TextBox3.Value) = 0 Then
    MsgBox "Please fill both date fields", vbInformation, "Date Range Error"
End If

As pointed out in this thread, you can also compare to vbNullString.

huangapple
  • 本文由 发表于 2023年7月27日 23:40:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76781402.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定