excel - VBA isString vs isNumeric
问题描述
我正在验证 Excel 中 VB 用户窗体中某些文本框中的数据。
数据可以是 6 位长的数字、至少 3 个字符长的字符串或字符串和数字的组合。
为此,我写道:
If Len(Trim(Me.TextBox1)) = 6 And IsNumeric(Trim(Me.TextBox1)) Then
(do operation)
Elseif Len(Trim(Me.TextBox1)) > 2 and IsString(Trim(Me.TextBox1)) Then
(do another operation)
Else
(do third operation)
End if
我可以让它工作isNumeric
,但似乎 VBA 不支持isString
。
有没有聪明的办法来解决这个问题?
解决方案
您可以调用工作表函数
Application.WorksheetFunction.IsText
不过,我可能会更自在地走线并使用 AscW 进行测试,如果期望 A-Za-z 则查找 65-90 和 95-122 范围内的值。
您绝对可以在以下方面进行改进。您可以参考ascii 代码来确定构成文本的可接受值。另请注意,网络上有大量功能可用于执行此任务。
Public Sub test()
Dim s As String, i As Long
s = "pr81"
For i = 1 To Len(s) ' 65-90 and 95-122.
Select Case True
Case (AscW(Mid$(s, i, 1)) >= 65 And AscW(Mid$(s, i, 1)) <= 90) Or _
(AscW(Mid$(s, i, 1)) >= 95 And AscW(Mid$(s, i, 1)) <= 122)
Case Else
MsgBox "Values are not all in range A-Za-z"
Exit For
End Select
Next i
End Sub
感谢@DirkReichel,这是一个非常简化的测试:
Option Explicit
Public Sub test()
Dim s As String, i As Long
s = "pr81"
For i = 1 To Len(s)
If LCase(Mid$(s, i, 1)) = UCase(Mid$(s, i, 1)) Then
MsgBox "Not all letters"
Exit For
End If
Next i
End Sub
推荐阅读
- flutter - 在下拉按钮输入框下显示错误文本
- sql - 如何使用在另一个表中预定义为外键的 id 属性填充表
- java - WFLYEJB0043:定时器的先前执行仍在进行中,跳过此重叠的计划执行
- node.js - 错误:InternalOpenIDError:无法验证断言(Steam)
- sql - 如何在表变量上添加非唯一索引
- javascript - 获取子字符串的值,然后用js中的另一个字符串替换它
- python - CUDA 运行时 API 错误 1:无效参数 (cudaMemcpy)
- kubernetes - 有人可以解释为什么我必须将外部(MetalLB、HAProxy 等)负载均衡器与裸机 kubernetes 集群一起使用吗?
- c++ - 这是在 TBB 错误中使用 volatile 吗?
- c++ - 为什么我所有的布尔值都设置为真?