我在excel电子表格中有一个列,其中包含每个单元格中已编号列表的字符串变量。例如,一个细胞可能包含:“橙色,蓝色,蓝色,白色,紫色”
我需要删除括号前的数字,并且不确定如何使用VBA来解决这个问题。
我需要的结果是:“橙色,蓝色,白色,紫色
提亚
发布于 2022-05-04 15:30:08
首先启用regex库
Tools Menu > References > Microsoft VBScript Regular ExpressionsSub demo()
Dim input_string as String
input_string = "1)orange 2)blue 3)white 4)purple"
Dim regex As Object
Set regex = New RegExp
' pattern will match one or more digits followed by a closing bracket (see https://regexr.com/)
regex.Pattern = "\d+\)"
' Global is set to true to replace all instances of the pattern that are found
regex.Global = True
result = regex.Replace(input_string , ")")
Debug.Print result
End Sub发布于 2022-05-04 15:46:06
如果列表项用相同的字符(空格?)分隔,我们可以从Split中获益。
Function RemoveNumbers(ByVal Text As String) As String
Dim Items As Variant, i%
Const sep = ")"
Items = Split(Text)
For i = LBound(Items) To UBound(Items)
Items(i) = sep & Split(Items(i), sep, 2)(1)
Next i
RemoveNumbers = Join(Items)
End Functionhttps://stackoverflow.com/questions/72115461
复制相似问题