我有一个Excel电子表格,其中有一个包含特定字符串的列。我需要在其他列中提取该字符串的所有部分。
.AN150417C65 ........... AN ...... 150417 ...... C ...... 65
.TLT150417C129 ...... ...... TLT ..... 150417 .... C ..... 129
.QQQ150417C106 ..... QQQ。 150417 ..... C ... 106
.SBNY150619C145 .... SBNY 150619 ..... C ... 145
.JAZZ150619C195 ..... JAZZ。 150619 .... C ... 195
.IBB150402P340 ....... IBB。 ... 150402 ... P ... 340
.IBB150410P337.5 ..... IBB .... 150410 .... P .... 337.5
请注意,总是有一个" C"或者" P"而且在字符串的开头还有一个点(我不需要)
感谢您的帮助!
答案 0 :(得分:1)
假设您的值在单元格A1:A7
中,那么此代码将删除组件部分并将它们放在B到E列中:
Sub SplitCodeIntoParts()
Dim regEx As Object, matches As Object, str As String, cl As Range
Set regEx = CreateObject("vbscript.regexp")
regEx.Pattern = "([a-z]+)([0-9]+)([a-z])(([0-9]+(\.[0-9]+)|[0-9]+))"
regEx.Global = True
regEx.IgnoreCase = True
For Each cl In Range("A1:A7")
str = VBA.Right$(cl, VBA.Len(cl) - 1)
Set matches = regEx.Execute(str)
If matches.Count <> 0 Then
cl.Offset(0, 1) = matches.Item(0).SubMatches(0)
cl.Offset(0, 2) = matches.Item(0).SubMatches(1)
cl.Offset(0, 3) = matches.Item(0).SubMatches(2)
cl.Offset(0, 4) = matches.Item(0).SubMatches(3)
End If
Next
End Sub
答案 1 :(得分:0)
如果你正在寻找一个公式,那将是丑陋的。这样的事情可以让你开始:
首先,使用:
添加一个隐藏列(我将以B为例) =Right(A1, Len(A1) - 1)
摆脱领先时期,然后
=IF(IsText(Left(B1,5)),"You have a 5 character code, do 5 character stuff here",If(IsText(Left(B1,4)), "You have a 4 character code", If(IsText(left(b1,3)),"You have a 3 character code", if(IsText(Left(b1,2)),"You have a 2 character code", If(IsText(Left(B1,1)),"You have a 1 character code","You don't have any leading characters)))))
然后开始填写文本字符串,并在这些条件下执行适当的处理。
它会很大,很难看,也很难拍摄,所以一次写一小块。
答案 2 :(得分:0)
这些适用于您的示例数据。
不确定你还有什么,但希望这会让你走上正轨:
=LEFT(A1,FIND("1",A1)-1)
=MID(A1,Find("1",A1),6)
=MID(A1,Find("1",A1)+6,1)
=RIGHT(A1,LEN(A1)-(FIND("1",A1)+6))
编辑:
如果您需要删除前导期间,则第一个公式变为:
=MID(A1,2,FIND("1",A1)-2)
答案 3 :(得分:0)
所以,这不是有史以来最漂亮的代码,但AFAIK应该能够胜任。如果它不起作用,请告诉我。这是假设字符串从Col A,第1行开始:
Sub Separate()
Dim parsePart As String, cel As Range, word As String, currCharacter As String, nextCharacter As String, prevCharacter As String
Dim j As Integer, i As Integer, value As Range, valueRow As Integer, valueCol As Integer, lastRow As Integer, k As Integer
' Assuming the list of words is in Column A, starting at Row 1
lastRow = Cells(1, 1).End(xlDown).Row
For Each value In Range(Cells(1, 1), Cells(lastRow, 1)).Cells
i = 0
parsePart = ""
prevCharacter = ""
currCharacter = ""
nextCharacter = ""
valueRow = value.Row
valueCol = value.Column
Set cel = Cells(valueRow, 1)
Debug.Print cel
word = cel.value
With cel.value
For j = 2 To Len(word) ' starting with the second character in the string - skipping the decimal
currCharacter = Mid(word, j, 1)
nextCharacter = Mid(word, j + 1, 1)
' Check to see if the current character is same type as next, if so, move on. Otherwise,
' print the current string to a new cell
If (IsNumeric(currCharacter) And Not IsNumeric(prevCharacter)) _
Or (Not IsNumeric(currCharacter) And IsNumeric(prevCharacter)) Or nextCharacter = "" Then
If nextCharacter = "" Then parsePart = parsePart & "" & currCharacter
i = i + 1
Cells(value.Row, valueCol + i).value = parsePart
parsePart = ""
End If
Debug.Print currCharacter
If Not IsNumeric(currCharacter) Then
parsePart = parsePart & "" & currCharacter
Debug.Print "Parsed word so far is: " & parsePart
ElseIf IsNumeric(currCharacter) Then
parsePart = parsePart & "" & currCharacter
Debug.Print "Parsed word so far is: " & parsePart
End If
prevCharacter = currCharacter
Next j
End With
Next value
End Sub
编辑:运行代码,它将分离数据int col。 B,C,D,E等
而且我也不知道你可以在VBA中做RegEx - 今天学到了新东西!