我有一个包含分配给像这样的项目的用户名的单元格
,FC757_random_name,AP372_another_one,FC782_again_different,FC082_samesamebutdifferent,
我只需要提取表达式开头的字母数字值,因此,
和_
之间的所有内容都是如此。
我让它适用于以下一个表达式,但我需要所有这些表达式。
= MID(A1;FIND(",";A1)+1;FIND("_";A1)-FIND(",";A1)-1)
我还修改了Text to Data,但是无法让它同时适用于多行。 理想情况下,这只适用于公式,但我猜(/恐惧)我需要VBA或宏,这是我以前从未使用过的。
所有帮助将不胜感激!
答案 0 :(得分:0)
这是基于regex的用户定义函数。
Option Explicit
Function extractMultipleExpressions(str As String, _
Optional delim As String = ", ")
Dim n As Long, nums() As Variant
Static rgx As Object, cmat 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
extractMultipleExpressions = vbNullString
With rgx
.Global = True
.MultiLine = False
.Pattern = "[A-Z]{2}[0-9]{3}"
If .Test(str) Then
Set cmat = .Execute(str)
'resize the nums array to accept the matches
ReDim nums(cmat.Count - 1)
'populate the nums array with the matches
For n = LBound(nums) To UBound(nums)
nums(n) = cmat.Item(n)
Next n
'convert the nums array to a delimited string
extractMultipleExpressions = Join(nums, delim)
End If
End With
End Function
答案 1 :(得分:0)
我相信您正在寻找类似的内容按Alt + F11
,然后选择插入>模块然后粘贴以下代码:
Public Function GetUsers(UserNameProject As String)
Dim userArray() As String
Dim users As String
Dim intPos As Integer
'this will split the users into an array based on the commas
userArray = Split(UserNameProject, ",")
'loop through the array and process any non blank element and extract user
'based on the position of the first underscore
For i = LBound(userArray) To UBound(userArray)
If Len(Trim(userArray(i))) > 0 Then
intPos = InStr(1, userArray(i), "_")
users = users & "," & Left(userArray(i), intPos - 1)
End If
Next
GetUsers = users
End Function
如果你的字符串在A1中,那么将=GetUsers(A1)
放在批准的单元格中即可使用。我认为这应该让你开始!
答案 2 :(得分:0)
要清除额外逗号的数据,请在单元格B1中使用此公式:
=TRIM(SUBSTITUTE(SUBSTITUTE(TRIM(SUBSTITUTE(SUBSTITUTE(A1;" ";"||");";";" "));" ";";");"||";" "))
然后在单元格C1中使用此公式并上下复制以从每个部分中提取所需的部分:
=IFERROR(INDEX(TRIM(LEFT(SUBSTITUTE(TRIM(MID(SUBSTITUTE($B1;";";REPT(" ";LEN($B1)));LEN($B1)*(ROW($A$1:INDEX($A:$A;LEN($B1)-LEN(SUBSTITUTE($B1;";";""))+1))-1)+1;LEN($B1)));"_";REPT(" ";LEN($B1)));LEN($B1)));COLUMN(A1));"")