Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 将正则表达式应用到文本框表单VBA中_Regex_Excel_Vba_Textbox - Fatal编程技术网

Regex 将正则表达式应用到文本框表单VBA中

Regex 将正则表达式应用到文本框表单VBA中,regex,excel,vba,textbox,Regex,Excel,Vba,Textbox,我需要你们的帮助。如何限制我的文本框?这是我下面的表格。 我需要的是为以下文本框创建if语句: 托盘ID:只能包含9个数字 纸箱编号:只能包含10个数字 连载(全部6):必须从FOC开始,且仅包含大写字母和数字[A-Z][0-9] 我用Javascript实现了这一点,但现在我需要在Excel工作表中进行备份。有什么想法吗 感谢您的关注,I'v创建了一个示例用户表单和一个命令按钮(如“确认”按钮),一旦按下,它将检查文本框中输入的所有值是否符合规则 下面的代码将让您开始,它检查“托盘ID”(9

我需要你们的帮助。如何限制我的文本框?这是我下面的表格。

我需要的是为以下文本框创建if语句: 托盘ID:只能包含9个数字 纸箱编号:只能包含10个数字 连载(全部6):必须从FOC开始,且仅包含大写字母和数字[A-Z][0-9]

我用Javascript实现了这一点,但现在我需要在Excel工作表中进行备份。有什么想法吗


感谢您的关注,

I'v创建了一个示例
用户表单
和一个
命令按钮
(如“确认”按钮),一旦按下,它将检查
文本框
中输入的所有值是否符合规则

下面的代码将让您开始,它检查“托盘ID”(9位)和“纸箱ID”(10位)中的值

代码

Option Explicit

Private Sub CommandButton1_Click()

Dim Reg1 As Object
Dim Reg2 As Object

Dim RegMatches As Object

' ====== Test PalletID_Tb =====
Set Reg1 = CreateObject("VBScript.RegExp")
With Reg1
    .Global = True
    .IgnoreCase = True
    .Pattern = "\d{9,9}" ' Match any set of 9 digits
End With

Set RegMatches = Reg1.Execute(Me.PalletID_Tb.Value)

If RegMatches.Count = 1 Then '<-- make sure there is only 1 match (9 digits and not 18 or 27)
    MsgBox "Value in Pallet ID is OK"
Else
    MsgBox "Pallet ID must have a 9 digit format"
    Me.PalletID_Tb.Value = ""
    Exit Sub
End If

' ====== Test CartonID_Tb =====
Set Reg2 = CreateObject("VBScript.RegExp")
With Reg2
    .Global = True
    .IgnoreCase = True
    .Pattern = "\d{10,10}" ' Match any set of 10 digits
End With

Set RegMatches = Reg2.Execute(Me.CartonID_Tb.Value)

If RegMatches.Count = 1 Then '<-- make sure there is only 1 match (10 digits and not 20)
    MsgBox "Value in Carton ID is OK"
Else
    MsgBox "Carton ID must have a 10 digit format"
    Me.CartonID_Tb.Value = ""
    Exit Sub
End If

' Do something if passed the 2 conditions

End Sub
选项显式
私有子命令按钮1_单击()
Dim Reg1作为对象
Dim Reg2作为对象
Dim RegMatches作为对象
'=======测试托盘=====
Set Reg1=CreateObject(“VBScript.RegExp”)
使用Reg1
.Global=True
.IgnoreCase=True
.Pattern=“\d{9,9}”匹配任何一组9位数字
以
Set RegMatches=Reg1.Execute(Me.PalletID_Tb.Value)

如果RegMatches.Count=1,则“为每个框设置
MaxLength
属性,并处理文本框事件,例如
txtpalleltid\u KeyDown
,如果您想截取单个键(并实际上阻止它们在第一位被输入),或
txtpalleltid_Exit
在焦点切换到另一个控件时验证内容(可能会取消它并强制焦点返回文本框);这样,您可以禁用
Agregar
按钮,直到知道所有字段都有效。。。但对于一个单一的问题来说,这是一个相当宽泛的主题。尝试一些东西,陷入困境,带着一个特定的问题回来=)让我试试吧,谢谢:)@Deluq阅读我的答案和下面的代码Hai,谢谢你的时间!