[英]How do i edit this VBA code to accept different values?
Having some trouble getting this online copypasta working correctly.在让这个在线 copypasta 正常工作时遇到了一些麻烦。 First character must be A to Z, second character 1 to 5, third character "" or 0 to 9.第一个字符必须是 A 到 Z,第二个字符 1 到 5,第三个字符“”或 0 到 9。
Box function is to specify a cell in Range A1:Z53. Box 功能是指定 Range A1:Z53 中的一个单元格。
Ideas on syntax?关于语法的想法?
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Select Case KeyAscii
Case Asc("0") To Asc("9")
Case Asc("-")
If Instr(1,Me.TextBox1.Text,"-") > 0 Or Me.TextBox1.SelStart > 0 Then
KeyAscii = 0
End If
Case Asc(".")
If InStr(1, Me.TextBox1.Text, ".") > 0 Then
KeyAscii = 0
End If
Case Else
KeyAscii = 0
End Select
End Sub
Thanks, but I got a good tip to use RegEx.谢谢,但我得到了一个使用 RegEx 的好提示。 This ended up being the best solution for my application.这最终成为我的应用程序的最佳解决方案。 After searching i found a previous answer that just needed the following modification to work in this scenario:搜索后,我找到了一个以前的答案,只需要进行以下修改即可在这种情况下工作:
.IgnoreCase = False
.Pattern = "[A-Z]([1-9]|[1-4][0-9]|[5][0-3])$"
Code can be found here: Apply Regex into a textbox form VBA代码可以在这里找到: Apply Regex into a textbox form VBA
VBA has limited pattern matching using the Like
operator : VBA 使用Like
运算符限制了模式匹配:
IsInRange = text Like "[A-Z][1-9]" Or text Like "[A-Z][1-4][0-9]" Or text Like "[A-Z]5[0-3]"
Another alternative can be using the Excel intersect method (will also work with for example " $Z$53
") :另一种选择是使用 Excel intersect 方法(也适用于例如“ $Z$53
”):
IsInRange = Not Intersect([A1:Z53], Range(text)) Is Nothing
or Excel range intersect operator :或 Excel 范围相交运算符:
IsInRange = Not Evaluate("ISERR(A1:Z53 " & text & ")")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.