excel - 为什么在尝试将布尔类型的变量传递给 txtBox_Exit() 事件时出现类型不匹配错误?
问题描述
我在 Excel 中搞乱了用户表单,遇到了第一个障碍,我无法用我自己的离线资源或谷歌搜索来解决。
我正在尝试处理用户对选定文本框的输入。我为 keydown 事件的这些文本框设置了一组事件,并且对此感到满意,直到我意识到许多最终用户会尝试使用他们的鼠标来导航表单,并且关闭鼠标单击是一个有点野蛮的修复问题。
结果,我尝试使用 keydown 的 keycode 方法捕获鼠标单击事件,但很快意识到,由于他们在单击期间从技术上离开文本框,因此处理当前正在使用的文本框退出的事件会更多优雅的。
这就是我的问题。
由于这两个事件非常相似,我试图捕获按键,如果它是一组键之一,我想调用退出子的代码。这样做的原因只是为了简化我的代码。我会使用一种方法来执行,但根据文本框会有一些变化。
按键捕获有效。但是,我输入的第二个
Call txtCurrentValue_Exit(cancel)
我收到类型不匹配错误,取消变量突出显示。
我一生都无法理解为什么我不能将布尔值解析为 msforms.returnsboolean 子。我知道这很可能是由于根本缺乏理解,但如果有人可以指导我正确的方式,那将是一个巨大的帮助。我不一定要寻找答案,但更多的是让我到达那里所需的思考过程。
下面有问题的代码。
目前的 Keydown 事件:
Private Sub txtCurrentValue_KeyDown(ByVal Keycode As MSForms.ReturnInteger, ByVal Shift As Integer)
If Keycode = vbKeyTab Or Keycode = vbKeyReturn Or Keycode = vbKeyUp Or Keycode = vbKeyDown Then
Dim cancel As Boolean
cancel = False
Call txtCurrentValue_Exit(cancel)
End Sub
目前的退出事件:
Private Sub txtCurrentValue_Exit(ByVal cancel As MSForms.ReturnBoolean)
'Storage variable for later calculations before punctuation added to variable value
'Checks to see if the textbox is empty after losing focus
If txtCurrentValue.TextLength > 0 Then
'If the value is not numeric, throw an error and reset the value
If IsNumeric(txtCurrentValue) = False Then
MsgBox ("Please ensure you only enter numeric values into this box.Punctuation such as £ or % signs will be added automatically.")
txtCurrentValue.Value = ""
Exit Sub
End If
'value confirmed as numeric, store in storage variable for later calculations
storedCurrentValue = txtCurrentValue.Value
'If the user has not elected to use decimal places, put a ".00" on the end of the string value for ease of reading. Also put a £ sign in front of the text.
If InStr(txtCurrentValue, ".") = False Then
txtCurrentValue.Value = txtCurrentValue + ".00"
End If
If InStr(txtCurrentValue, "£") = False Then
txtCurrentValue.Value = "£" + txtCurrentValue
End If
txtBridgeOffer.SetFocus
End If
End Sub
变量storedCurrentValue
和方法IsNumeric
是独立工作的。
解决方案
但是对于验证,您只需要该_exit
事件。不需要 keydown 事件。无论用户通过鼠标还是键盘尝试退出文本框,都会调用 exit 事件。
如果您Cancel = True
在该事件中设置,则意味着文本框的退出操作将被取消,光标将停留在该文本框中。
Option Explicit
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Dim ValidInput As Boolean
'do some validation here
If TextBox1 = "aa" Then
ValidInput = True
End If
If ValidInput = False Then
MsgBox "Only 'aa' is a valid input."
Cancel = True 'this line will cancel the exit of the textbox and the cursor will set back to that box
End If
End Sub
例如,如果该文本框不包含 ,则您不能离开/退出该文本框aa
。这是您唯一需要的活动。
补充说明:
您可能正在寻找这样的东西:
Option Explicit
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
With TextBox1 '<-- Your textbox name here
If .TextLength > 0 Then
If Not IsNumeric(.Value) Then
MsgBox ("Please ensure you only enter numeric values into this box.Punctuation such as £ or % signs will be added automatically.")
.Value = ""
Cancel = True
Exit Sub
End If
If InStr(.Value, ".") = 0 Then
.Value = .Value & ".00"
End If
If InStr(.Value, "£") = 0 Then
.Value = "£" & .Value
End If
End If
End With
End Sub
请注意,InStr 函数不返回布尔值True
/False
而是返回第一个查找的位置。所以你需要测试If InStr(.Value, ".") = 0
,因为0
意味着没有找到。
另请注意,如果您将数字输入值更改为 eg 12
,£12.00
然后用户返回将其更改为 eg £13.00
,它将不会被接受为有效,因为它不是数字。所以你也必须允许。看看如何使用正则表达式检查模式,这对于验证非常方便。
这是一个RegEx 模式的示例,用于检查文本是否看起来像£12.00
.
该模式^£[0-9]+\.[0-9]{2}$
将匹配所有...</p>
^£
- 开始于£
[0-9]+
- 后跟 0-9 的一位或多位数字\.
- 后跟一个点.
[0-9]{2}$
- 以 0-9 的 2 位数字结尾
推荐阅读
- bash - 将“bc”外部工具转换为函数
- iis - 找不到localtest.me的服务器IP地址
- python - 用 numpy 创建大向量
- python - Kubernetes Python Client Watch 停止返回结果,不会失败
- c# - 处理 post 请求正文中的枚举
- javascript - 在子目录中为 React 应用程序(+Router)配置 .htaccess
- video - 有没有办法防止在 HTML5 视频播放器中下载视频
- c++ - 从 cin 读取的进程上的 HUP 信号
- c++ - 为什么我的 3-Partitioning 问题无法通过给定的测试用例?(3-分区问题)
- php - 注释包含 # / 字符的 PHP 行