将正则表达式应用到文本框表单 VBA [英] Apply Regex into a textbox form VBA

查看:37
本文介绍了将正则表达式应用到文本框表单 VBA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

伙计们,我需要你们的帮助.如何限制我的文本框?这是我的以下表格.

I need your help guys. How can I limit my textboxes? This is my Form Below.

我需要为以下文本框创建 if 语句:Pallet ID:只能包含 9 个数字纸箱 ID:只能包含 10 个数字连续剧(全部 6 个):必须以 FOC 开头,并且只包含大写字母和数字 [A-Z][0-9]

What I need is to create if statement for the following textboxes: Pallet ID: Must only contain 9 numbers Carton ID: Must only contain 10 numbers Serials (all 6): Must startwith FOC and only contain CAPS and Numbers [A-Z][0-9]

我是用 Javascript 完成的,但现在我需要在 excelsheet 中进行备份.有什么想法吗?

I did this in Javascript but now I need to have a backup in a excelsheet. Any ideas?

感谢您的关注,

推荐答案

我创建了一个示例 User_Form 和一个 CommandButton(类似于确认"按钮)按下后,它会检查 TextBox 中输入的所有值是否符合规则.

I'v created a sample User_Form and a CommandButton (like a "confirm" button) that's once it's pressed it checks that all the values entered in the TextBoxes are according to the rules.

下面的代码将帮助您开始,它会检查托盘 ID"(9 位数字)和纸箱 ID"(10 位数字)中的值.

The code below will get you started, it checks the values in "Pallet ID" (9-digits) and "Carton ID" (10-digits).

代码

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

这篇关于将正则表达式应用到文本框表单 VBA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆