使用 VBA 解析和拆分带通配符的字符串?
Using VBA to parse and split a string with wildcards?
我有一个 sheet,其中包含字母数字字符的项目编号,以及一行中的一堆其他信息。有时,相似的项目合并成一行,项目编号的差异将显示为 (X/Y) 以选择在项目编号中该点使用的字符(不只是 X 或 Y,可以是任何字母数字字符)。换句话说,这些条目将如下所示:
AB(X/Y)CD123
我需要的是一种将其分为两个项目编号 ABXCD123 和 ABYCD123 的方法。之后,我必须在当前行下方创建一行并将当前行复制到其中,并使用更改后的项目编号,但这部分很容易。我试过使用 InStr 来标记 (X/Y),但我不知道如何提取 X 和 Y 字符来用它们创建新字符串。我也不知道通配符是否适用于 InStr,而且我对 RegEx 不太熟悉。
有什么想法吗?
这里是对 UDF² 中的正则表达式¹ 的简要介绍。
Function partNums(str As String, _
Optional num As Integer = 1)
Dim tmp As String
Static rgx As Object
'with rgx as static, it only has to be created once; beneficial when filling a long column with this UDF
If rgx Is Nothing Then
Set rgx = CreateObject("VBScript.RegExp")
End If
partNums = vbNullString
With rgx
.Global = True
.IgnoreCase = True
.MultiLine = False
.Pattern = "\([A-Z]{1}/[A-Z]{1}\)"
If .Test(str) Then
tmp = .Execute(str)(0)
Select Case num
Case 2
tmp = Mid(tmp, 4, 1)
Case Else
tmp = Mid(tmp, 2, 1)
End Select
partNums = .Replace(str, tmp)
End If
End With
End Function
在B2:B3中,
=partNums(A2)
=partNums(A3,2)
这是一个处理 1 到 3 个字符的大部分重复的 UDF。
Function partNums(str As String, _
Optional num As Integer = 1)
Dim tmp As String
Static rgx As Object
'with rgx as static, it only has to be created once; beneficial when filling a long column with this UDF
If rgx Is Nothing Then
Set rgx = CreateObject("VBScript.RegExp")
End If
partNums = vbNullString
With rgx
.Global = True
.IgnoreCase = True
.MultiLine = False
.Pattern = "\([A-Z]{1,3}/[A-Z]{1,3}\)"
If .Test(str) Then
tmp = .Execute(str)(0)
tmp = Split(Replace(Replace(tmp, Chr(40), vbNullString), Chr(41), vbNullString), Chr(47))(num - 1)
partNums = .Replace(str, tmp)
End If
End With
End Function
¹ regex questions can usually be answered by the solutions in How to use Regular Expressions (Regex) in Microsoft Excel both in-cell and loops.
² 用户定义函数(又名 UDF)被放入标准模块代码 sheet。点击 Alt+F11,当 VBE 打开时,立即使用 pull-down 菜单 Insert ► Module (Alt+I,M)。将函数代码粘贴到新模块代码 sheet 中,标题类似于 Book1 - Module1 (Code)。点按 Alt+Q 以 return 进入你的工作sheet(s).
我有一个 sheet,其中包含字母数字字符的项目编号,以及一行中的一堆其他信息。有时,相似的项目合并成一行,项目编号的差异将显示为 (X/Y) 以选择在项目编号中该点使用的字符(不只是 X 或 Y,可以是任何字母数字字符)。换句话说,这些条目将如下所示:
AB(X/Y)CD123
我需要的是一种将其分为两个项目编号 ABXCD123 和 ABYCD123 的方法。之后,我必须在当前行下方创建一行并将当前行复制到其中,并使用更改后的项目编号,但这部分很容易。我试过使用 InStr 来标记 (X/Y),但我不知道如何提取 X 和 Y 字符来用它们创建新字符串。我也不知道通配符是否适用于 InStr,而且我对 RegEx 不太熟悉。
有什么想法吗?
这里是对 UDF² 中的正则表达式¹ 的简要介绍。
Function partNums(str As String, _
Optional num As Integer = 1)
Dim tmp As String
Static rgx As Object
'with rgx as static, it only has to be created once; beneficial when filling a long column with this UDF
If rgx Is Nothing Then
Set rgx = CreateObject("VBScript.RegExp")
End If
partNums = vbNullString
With rgx
.Global = True
.IgnoreCase = True
.MultiLine = False
.Pattern = "\([A-Z]{1}/[A-Z]{1}\)"
If .Test(str) Then
tmp = .Execute(str)(0)
Select Case num
Case 2
tmp = Mid(tmp, 4, 1)
Case Else
tmp = Mid(tmp, 2, 1)
End Select
partNums = .Replace(str, tmp)
End If
End With
End Function
在B2:B3中,
=partNums(A2)
=partNums(A3,2)
这是一个处理 1 到 3 个字符的大部分重复的 UDF。
Function partNums(str As String, _
Optional num As Integer = 1)
Dim tmp As String
Static rgx As Object
'with rgx as static, it only has to be created once; beneficial when filling a long column with this UDF
If rgx Is Nothing Then
Set rgx = CreateObject("VBScript.RegExp")
End If
partNums = vbNullString
With rgx
.Global = True
.IgnoreCase = True
.MultiLine = False
.Pattern = "\([A-Z]{1,3}/[A-Z]{1,3}\)"
If .Test(str) Then
tmp = .Execute(str)(0)
tmp = Split(Replace(Replace(tmp, Chr(40), vbNullString), Chr(41), vbNullString), Chr(47))(num - 1)
partNums = .Replace(str, tmp)
End If
End With
End Function
¹ regex questions can usually be answered by the solutions in How to use Regular Expressions (Regex) in Microsoft Excel both in-cell and loops.
² 用户定义函数(又名 UDF)被放入标准模块代码 sheet。点击 Alt+F11,当 VBE 打开时,立即使用 pull-down 菜单 Insert ► Module (Alt+I,M)。将函数代码粘贴到新模块代码 sheet 中,标题类似于 Book1 - Module1 (Code)。点按 Alt+Q 以 return 进入你的工作sheet(s).