如何使用 InputBox 的空文本框来区分 "cancel" 和 "OK" 之间的区别?
How to tell the difference between "cancel" and "OK" with an empty textbox of an InputBox?
A VBA inputBox returns 按下取消按钮时的空字符串。
当按下 OK 时,它的文本框中的文本。
inputBox 的第三个位置参数是其文本框中的初始文本。此参数的默认值为“”。
当用户单击“添加记录”按钮时,我使用输入框要求用户指定新记录的名称。如果他按“取消”,我退出子。
如果他输入名字失败,或者输入名字后删除,我想要一个 msgBox 告诉他必须指定一个唯一的记录名称。
如何区分空文本框的“取消”和“确定”?
我发现了几个类似的问题,但 none 解决了我的问题。
有一种方法可以检查用户是否单击了 "Cancel" 或只是输入了空字符串。
试试这个:
test = InputBox("Enter a value")
If StrPtr(test) = 0 Then
MsgBox "Cancel"
ElseIf Len(test) = 0 Then
MsgBox "No entry"
Else
MsgBox "Correct"
End If
然而,这是一个非常粗略的解决方案。您可以在此处阅读有关 StrPtr 函数的更多信息:
我相信下面的代码将为您提供一个可靠的解决方案。有关 Application.InputBox 方法和函数的更多信息,请访问 Microsoft Docs。
Option Explicit
'Type Parameter of the Application.InputBox method tells
'the method to return a value of that type. Below I've shown
'having it return a text string
Public Const A_TEXT_STRING As Variant = 2
Sub Button1_Click()
Dim myVal As Variant
myVal = Application.InputBox("Enter Record Name", _
Title:="RECORD NAME", _
Type:=A_TEXT_STRING)
'If myVal equals the vbNullString type then
'the person pressed OK without entering any data
If myVal = vbNullString Then
MsgBox "Please Select a record", Buttons:=vbOKOnly, Title:="SELECT RECORD"
'if myVal is equal to False (boolean value) then the user pressed
'the Cancel button. By checking for not equal, that means that the user pressed
'Ok and entered a value. You'll need to handle the condition of valid values
ElseIf myVal <> False Then
Range("A1").Value2 = myVal
End If
End Sub
感谢您一直以来对我的帮助...给您。在这种情况下是一个 YES 和 NO 的文本框。如果你想确定并取消,你将不得不看到这个:
我的情况是 vbYesNo, 你的情况是 vbOKCancel
OK return 值为:1
取消return值为:2
Dim control As String
control = Empty
control = MsgBox("Are you sure ??", vbYesNo, "System warning")
If control = 6 Then
' code if answer was YES
ElseIf control = 7 Then
' code if answer was NO
End If
A VBA inputBox returns 按下取消按钮时的空字符串。
当按下 OK 时,它的文本框中的文本。
inputBox 的第三个位置参数是其文本框中的初始文本。此参数的默认值为“”。
当用户单击“添加记录”按钮时,我使用输入框要求用户指定新记录的名称。如果他按“取消”,我退出子。
如果他输入名字失败,或者输入名字后删除,我想要一个 msgBox 告诉他必须指定一个唯一的记录名称。
如何区分空文本框的“取消”和“确定”?
我发现了几个类似的问题,但 none 解决了我的问题。
有一种方法可以检查用户是否单击了 "Cancel" 或只是输入了空字符串。 试试这个:
test = InputBox("Enter a value")
If StrPtr(test) = 0 Then
MsgBox "Cancel"
ElseIf Len(test) = 0 Then
MsgBox "No entry"
Else
MsgBox "Correct"
End If
然而,这是一个非常粗略的解决方案。您可以在此处阅读有关 StrPtr 函数的更多信息:
我相信下面的代码将为您提供一个可靠的解决方案。有关 Application.InputBox 方法和函数的更多信息,请访问 Microsoft Docs。
Option Explicit
'Type Parameter of the Application.InputBox method tells
'the method to return a value of that type. Below I've shown
'having it return a text string
Public Const A_TEXT_STRING As Variant = 2
Sub Button1_Click()
Dim myVal As Variant
myVal = Application.InputBox("Enter Record Name", _
Title:="RECORD NAME", _
Type:=A_TEXT_STRING)
'If myVal equals the vbNullString type then
'the person pressed OK without entering any data
If myVal = vbNullString Then
MsgBox "Please Select a record", Buttons:=vbOKOnly, Title:="SELECT RECORD"
'if myVal is equal to False (boolean value) then the user pressed
'the Cancel button. By checking for not equal, that means that the user pressed
'Ok and entered a value. You'll need to handle the condition of valid values
ElseIf myVal <> False Then
Range("A1").Value2 = myVal
End If
End Sub
感谢您一直以来对我的帮助...给您。在这种情况下是一个 YES 和 NO 的文本框。如果你想确定并取消,你将不得不看到这个: 我的情况是 vbYesNo, 你的情况是 vbOKCancel OK return 值为:1 取消return值为:2
Dim control As String
control = Empty
control = MsgBox("Are you sure ??", vbYesNo, "System warning")
If control = 6 Then
' code if answer was YES
ElseIf control = 7 Then
' code if answer was NO
End If