我以这种方式有一个字符串
"来自东京的汽车 5 33:53:03 到达。13:56:39 奥地利汽车 5 33:53:07 到达。13:56:43 印度汽车 5 33:53:03 到达时间 13:56:39"
在excel的同一个单元格中。
我需要将字符串的这些部分at 13:56:39
at 13:56:43
13:56:39
显示在单独的单元格中。
请帮忙
我会有一个不同的方法,使用一个公式:
B 列使用以下公式:
B1=IFERROR(SEARCH("at ??:??:??",A$1,1),"")
B2=IFERROR(SEARCH("at ??:??:??",A$1,B1+11),"")
C 列使用以下公式:
C1=IFERROR(PART(A$1,B1,11),"")
这些将适用于大量事件。
如果您的数据在单列中,我认为Regexp
带有变体数组的 a 是有意义的。
但作为一个更灵活的选项您可以使用以下 UDF 作为数组 - 输入公式来拆分字符串
如果您的字符串被输入A1
并且您预计最多有 5 个匹配项
额外的不匹配将有#N/A
代码
updated to handle single matches
Function AtTime(strIN As String) As Variant
Dim objRegex As Object
Dim objRMC As Object
Dim strOut() As String
Dim lngCnt As Long
Set objRegex = CreateObject("vbscript.regexp")
With objRegex
.Global = True
.Pattern = "at \d{2}:\d{2}:\d{2}"
If .test(strIN) Then
Set objRMC = .Execute(strIN)
If objRMC.Count > 1 Then
ReDim strOut(1 To objRMC.Count)
For lnGCnt = 1 To UBound(strOut)
strOut(lnGCnt) = objRMC(lnGCnt - 1)
Next
Else
'handle single matches
ReDim strOut(1 To 2)
strOut(1) = objRMC(0).Value
strOut(2) = "#N/A"
End If
AtTime = strOut
Else
AtTime = "no match"
End If
End With
End Function
Down and dirty string manipulation:
Option Explicit
Sub Test()
Dim cellValue As String
Dim newCellArray() As String
Dim valueYouWant As String
Dim cellCounter As Integer
Dim x As Integer
Dim myRange As Range
Const SEPERATOR_VALUE = "at "
Const ASCII_A = 65
For cellCounter = 1 To 10 '10 represents the last row...there are ways to set this dynamically if needed
cellValue = Sheet1.Range("A" & cellCounter)
newCellArray = Split(cellValue, "at ")
'Array is zero-based, but we want to start at first split value
For x = 1 To UBound(newCellArray)
valueYouWant = Trim$(Left$(newCellArray(x), InStr(1, newCellArray(x), " "))) 'You could prefix this with "at " if you really needed it
Sheet1.Range(Chr$(ASCII_A + x) & cellCounter).Value = valueYouWant
Next x
Next cellCounter
End Sub