为什么 IsNull()、IsEmpty()、Application.WorksheetFunction.Istext() 在组合框上不起作用?
How come IsNull(), IsEmpty(), Application.WorksheetFunction.Istext() does not work on combo boxes?
我已经尝试了下面的几种变体,即使我不 select 或在组合框中键入任何内容,MsgBox 也不会运行。
变体 1:
Private Sub CommandButton1_Click()
If IsNull(cmbPaidTo.Text) = True Then
MsgBox "Payee cannot be empty."
End If
Unload Me
UserForm1.Show
End Sub
变体 2:
Private Sub CommandButton1_Click()
If IsNull(cmbPaidTo) = True Then
MsgBox "Payee cannot be empty."
End If
Unload Me
UserForm1.Show
End Sub
变体 3:
Private Sub CommandButton1_Click()
If IsEmpty(cmbPaidTo.Text) = True Then
MsgBox "Payee cannot be empty."
End If
Unload Me
UserForm1.Show
End Sub
变体 4:
Private Sub CommandButton1_Click()
If IsEmpty(cmbPaidTo) = True Then
MsgBox "Payee cannot be empty."
End If
Unload Me
UserForm1.Show
End Sub
变体 5:
Private Sub CommandButton1_Click()
If Application.WorksheetFunction.IsText(cbxPaidTo.Text) = False Then
MsgBox "Payee cannot be empty."
End If
Unload Me
UserForm1.Show
End Sub
变体 6:
Private Sub CommandButton1_Click()
If Application.WorksheetFunction.IsText(cbxPaidTo) = False Then
MsgBox "Payee cannot be empty."
End If
Unload Me
UserForm1.Show
End Sub
提交表单时组合框永远不能为空,但我不明白为什么我无法让它工作。
这是我的用户窗体的样子:
我指的是没有文本标签的组合框。
尝试:
If len(cmbPaidTo) = 0 Then
组合框的值将始终为字符串 ""
IsNull()
应仅用于 Access 查询。输入框的值不能是Null
.
IsEmpty()
- Returns 一个布尔值,表示变量是否已经初始化。主要用于 Excel 中的范围和单元格。 MSDN。 cbxPaidTo.Text
不能为空,因为它是一个初始化对象。
.IsText()
- 这里的情况有点不同。即使是空字符串 ""
仍然被视为文本。每当您的表单中没有任何内容时,它都会返回一个空字符串。检查这个:
Sub TestMe()
Debug.Print WorksheetFunction.IsText("")
End Sub
一个可能的解决方案是在修剪后检查输入 的大小。像这样:
If Trim(Len(cmbPaidTo) Then
我已经尝试了下面的几种变体,即使我不 select 或在组合框中键入任何内容,MsgBox 也不会运行。
变体 1:
Private Sub CommandButton1_Click()
If IsNull(cmbPaidTo.Text) = True Then
MsgBox "Payee cannot be empty."
End If
Unload Me
UserForm1.Show
End Sub
变体 2:
Private Sub CommandButton1_Click()
If IsNull(cmbPaidTo) = True Then
MsgBox "Payee cannot be empty."
End If
Unload Me
UserForm1.Show
End Sub
变体 3:
Private Sub CommandButton1_Click()
If IsEmpty(cmbPaidTo.Text) = True Then
MsgBox "Payee cannot be empty."
End If
Unload Me
UserForm1.Show
End Sub
变体 4:
Private Sub CommandButton1_Click()
If IsEmpty(cmbPaidTo) = True Then
MsgBox "Payee cannot be empty."
End If
Unload Me
UserForm1.Show
End Sub
变体 5:
Private Sub CommandButton1_Click()
If Application.WorksheetFunction.IsText(cbxPaidTo.Text) = False Then
MsgBox "Payee cannot be empty."
End If
Unload Me
UserForm1.Show
End Sub
变体 6:
Private Sub CommandButton1_Click()
If Application.WorksheetFunction.IsText(cbxPaidTo) = False Then
MsgBox "Payee cannot be empty."
End If
Unload Me
UserForm1.Show
End Sub
提交表单时组合框永远不能为空,但我不明白为什么我无法让它工作。
这是我的用户窗体的样子:
我指的是没有文本标签的组合框。
尝试:
If len(cmbPaidTo) = 0 Then
组合框的值将始终为字符串 ""
IsNull()
应仅用于 Access 查询。输入框的值不能是Null
.IsEmpty()
- Returns 一个布尔值,表示变量是否已经初始化。主要用于 Excel 中的范围和单元格。 MSDN。cbxPaidTo.Text
不能为空,因为它是一个初始化对象。.IsText()
- 这里的情况有点不同。即使是空字符串""
仍然被视为文本。每当您的表单中没有任何内容时,它都会返回一个空字符串。检查这个:
Sub TestMe()
Debug.Print WorksheetFunction.IsText("")
End Sub
一个可能的解决方案是在修剪后检查输入
If Trim(Len(cmbPaidTo) Then