VBA – 抛出exception,以获取更多的数字输出错误
我有强制任何CT值的长度为“CT-”之后的4个数字的长度的代码,并且在“TL-”之后以TL值执行长度为6的相同的代码。 如果太短,在“TL-”之后加0。 如果太长,则从“TL-”之后的右边删除0; CT也是一样。
我碰到的问题是,我需要抓住CT值后面的短划线后面的最大两个数字。 它只需要在该短划线之后,只抓取紧接的值或者将它们连接在一起。
我以前的问题解决“TL-”代码的问题在这里,如果这是有用的。
例:
当前输出
Start: Output: CT-0087(TC-7988) CT-0087 CT-0067-02 CT-0067 CT-0076-REV01 CT-0076 CT-0098-1 A CT-0098
期望的输出
Start: Desired Output: CT-0087(TC-7988) CT-0087 CT-0067-02 CT-0067-02 CT-0076-REV01 CT-0076-01 CT-0098-1 A CT-0098-1
所以应该总是有一个“ – ”和最多2个数字来抓取,但是我只想要抓住它,如果破折号紧随其后(对于潜在的错误:CT-0087不应该成为CT-877988,因为抓取数字或数字破折号后),我不知道如何抛出一个特殊问题的例外。 任何想法都会非常有帮助!
在代码中:
'force length of TL/CT to be 6/4 numbers long, eliminate spaces Dim str As String, ret As String, k As Integer For k = 2 To StartSht.Range("C2").End(xlDown).Row ret = "" str = StartSht.Range("C" & k).Value ret = ExtractNumberWithLeadingZeroes(str, "TL", 6) If ret <> "" Then StartSht.Range("C" & k).Value = "TL-" & ret Else 'for CT numbers ret = ExtractNumberWithLeadingZeroes(str, "CT", 4) If ret <> "" Then StartSht.Range("C" & k).Value = "CT-" & ret End If End If Next k
function:
Public Function ExtractNumberWithLeadingZeroes(ByRef theWholeText As String, ByRef idText As String, ByRef numCharsRequired As Integer) As String ' Finds the first entry of idText in theWholeText ' Returns the first number found after idText formatted ' with leading zeroes Dim i As Integer Dim j As Integer Dim thisChar As String Dim returnValue As String Dim tmpText As String Dim firstPosn As Integer Dim secondPosn As Integer returnValue = "" firstPosn = InStr(1, theWholeText, idText) If firstPosn > 0 Then ' remove any text before first idText, also remove the first idText tmpText = Mid(theWholeText, firstPosn + Len(idText)) 'if more than one idText value, delete everything after (and including) the second idText secondPosn = InStr(1, tmpText, idText) If secondPosn > 0 Then tmpText = Mid(tmpText, 1, secondPosn) End If ' Find first number For j = 1 To Len(tmpText) If IsNumeric(Mid(tmpText, j, 1)) Then tmpText = Mid(tmpText, j) Exit For End If Next j ' Find where the numbers end returnValue = tmpText For j = 1 To Len(returnValue) thisChar = Mid(returnValue, j, 1) If Not IsNumeric(thisChar) Then returnValue = Mid(returnValue, 1, j - 1) Exit For End If Next j 'force to numCharsRequired numbers if too short; add 0s immediately after idText 'force to numCharsRequired numbers if too long; eliminate 0s immediately after idText ' The CLng gets rid of leading zeroes and the Format$ adds any required up to numCharsRequired chars returnValue = Format$(CLng(returnValue), String(numCharsRequired, "0")) End If ExtractNumberWithLeadingZeroes = returnValue End Function
这是一个函数,将返回您在上面指定的内容:
===========================================
Option Explicit Function ExtractCode(S As String) As String Dim RE As Object, MC As Object Set RE = CreateObject("vbscript.regexp") With RE .Global = False .ignorecase = False 'could be true if you want .Pattern = "(CT-)\d*?(\d{4})(?!\d)(?:(-)\D*(\d{1,2}))?.*" S = Replace(S, "CT-", "CT-000") 'add leading zero's to pad to 4 if necessary If .test(S) = True Then ExtractCode = .Replace(S, "$1$2$3$4") Else ExtractCode = "" End If End With
结束function
这里有些例子:
这里是对正则expression式的正式简要的解释:
(CT – )\ d *(\ d {1,4})(\ d?!)?(?:( – )\ d *(\ d {1,2}))?*
(CT-)\d*?(\d{1,4})(?!\d)(?:(-)\D*(\d{1,2}))?.*
选项:区分大小写; ^ $在换行符不匹配
- 匹配下面的正则expression式并将其匹配到反向引用编号1
(CT-)
- 匹配string“CT-”逐字
CT-
- 匹配string“CT-”逐字
- 匹配一个“数字”
\d*?
的单个字符- 在零和无限次之间,尽可能less的次数,按需扩展(懒惰)
*?
- 在零和无限次之间,尽可能less的次数,按需扩展(懒惰)
- 匹配下面的正则expression式并将其匹配到反向引用编号2
(\d{1,4})
- 匹配一个“数字”
\d{1,4}
- 在1到4次之间,尽可能多地按需要回馈(贪婪)
{1,4}
- 在1到4次之间,尽可能多地按需要回馈(贪婪)
- 匹配一个“数字”
- 断言从这个位置(负向前视)
(?!\d)
开始,不可能匹配下面的正则expression式,- 匹配一个“数字”
\d
的单个字符
- 匹配一个“数字”
- 匹配下面的正则expression式
(?:(-)\D*(\d{1,2}))?
- 在零次和一次之间,尽可能多次,根据需要回馈(贪婪)
?
- 匹配下面的正则expression式并将其匹配到反向引用编号3
(-)
- 匹配字符“ – ”字面意思
-
- 匹配字符“ – ”字面意思
- 匹配不是“数字”的单个字符
\D*
- 在零和无限次之间,尽可能多次,根据需要回馈(贪婪)
*
- 在零和无限次之间,尽可能多次,根据需要回馈(贪婪)
- 匹配下面的正则expression式并将其匹配到反向引用编号4
(\d{1,2})
- 匹配一个“数字”
\d{1,2}
- 1到2次之间,尽可能多地按需要回馈(贪婪)
{1,2}
- 1到2次之间,尽可能多地按需要回馈(贪婪)
- 匹配一个“数字”
- 在零次和一次之间,尽可能多次,根据需要回馈(贪婪)
- 匹配任何不是换行符的单个字符
.*
- 在零和无限次之间,尽可能多次,根据需要回馈(贪婪)
*
- 在零和无限次之间,尽可能多次,根据需要回馈(贪婪)
$ 1 $ 2 $ 3 $ 4
- 通过捕获组号1
$1
插入上次匹配的文本 - 通过捕获组号2
$2
插入上次匹配的文本 - 通过捕获组号3
$3
插入最后匹配的文本 - 通过捕获组号4
$4
插入上次匹配的文本
用RegexBuddy创build