简体   繁体   English

如何编辑此 VBA 代码以接受不同的值?

[英]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.

相关问题 如何编辑 VBA 代码以粘贴到不同的位置 - How to edit VBA code to paste in a different location 如何将此 VBA 代码更改为仅粘贴值 - How do I change this VBA code to Paste values only 如果满足条件,我如何从 excel 编辑我的 VBA 代码以在主题中包含特定行? - How do i edit my VBA Code from excel to include a specific line in the subject if conditions are met? 我需要使用VBA代码安排不同值的帮助 - I need help arranging different values using vba code 如何使用 Excel VBA 设置变量以筛选不同的文本字符串值? - How do I set a variable to filter by for different text string values using Excel VBA? 在使用VBA的Excel中,如何分割由不同分隔符分隔的多个值(即:逗号,冒号等) - In Excel using VBA how do I split multiple values seperated by different seperators (ie: commas, colons, etc) 如何在 VBA 的不同工作表中将一系列单元格值设置为另一个单元格范围? - How do I set a range of cell values to another range of cells in a different worksheet in VBA? 我如何在VBA中找到字符串或值 - how do i find a string or values in vba 如何使用For循环在三个不同的范围内提高VBA代码的效率? - How do I make my VBA code more efficient using For each loops for three different ranges? 如何为30种不同的Excel工作簿运行相同的宏,VBA代码? - How do I run the same macro, VBA-code for 30 different excel workbooks?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM