您好我有一个Excel电子表格,它在一个单元格中包含以下格式的行。
[2013-12-01 00:29:36.45] ALL 000000000000 GLOBAL_SCOPE AUDIT: User [XXXXXX04] logged off.
[2013-12-01 00:29:55.292] ALL 000000000000 GLOBAL_SCOPE AUDIT: User [XXXXX05] authenticated via public key.
[2013-12-01 00:29:55.736] ALL 000000000000 GLOBAL_SCOPE AUDIT: User [xxx03] is opening file [/Inbox/AXS02XXXXXXX.AXS_RECON.IRF7171_EFTDR20131130.txt.p7] for transfer.
[2013-12-01 00:30:02.453] ALL 000000000000 GLOBAL_SCOPE AUDIT: User [xxxx05] authenticated via public key.
[2013-12-01 00:30:35.387] ALL 000000000000 GLOBAL_SCOPE AUDIT: User [sfdsf03] logged off.
我想仅从包含“收件箱”一词的行中提取3个值。基本上我想要的ouptut如下,在3列。
例如: -
00:29:55 XXXXXX03 AXS02XXXXXXX.AXS_RECON.IRF7171_EFTDR20131130.txt.p7
请注意,用户名,即User [x]值不同,每行的长度不同。请帮忙。
谢谢!
答案 0 :(得分:0)
如果您已将数据存储在Excel工作表中,我想知道为什么需要VBA。您也可以使用以下公式:
=IF(ISERROR(FIND("Inbox";A1));
"";
MID(A1;13;8)&" "&
MID(A1;FIND("User";A1)+6;FIND("]";A1;FIND("User";A1))-FIND("User";A1)-6)&" "&
MID(A1;FIND("Inbox";A1)+6;FIND("]";A1;FIND("Inbox";A1))-FIND("Inbox";A1)-6)
)
如果文本中有“收件箱”一词,那么您使用三次MID
来获取文本中的三个部分。要查找正在使用MID
的{{1}}的正确起点和字符数。
如果您仍想在VBA中执行此操作,则解决方案看起来很相似,只需要使用FIND
而不是InStr
。
答案 1 :(得分:0)
这是两个VBA解决方案。第一个是用户定义函数,它将所需的段提取到公式所在的单元格中。代码中定义了不同的索引。
第二个是宏,它处理A列中的条目,从A2开始,并将这三个段解析为相邻的三个列。不确定哪个更适合你。
他们都使用正则表达式来获得不同的子串。
两者都进入常规模块。
<强>功能:强>
Option Explicit
'Returns the three components
' Index 1 = Time
' Index 2 = User
' Index 3 = inbox file
Function ParseInbox(S As String, Index As Long) As Variant
Dim RE As Object, MC As Object
'Check that Index is proper
If Index < 1 Or Index > 3 Then
ParseInbox = CVErr(xlErrNum)
Exit Function
End If
Set RE = CreateObject("vbscript.regexp")
With RE
.ignorecase = True
.MultiLine = True
.Pattern = "^.*((?:\d{2}:\d{2}:)\d{2}).*User\s*\[([^]]+).*Inbox/([^]]+)"
If .test(S) = True Then
Set MC = .Execute(S)
ParseInbox = MC(0).submatches(Index - 1)
End If
End With
End Function
<强>宏:强>
Sub ProcInbox()
'Assumes Data in Column A; starts in A2, and
' to be split into columns B, C & D
Dim RE As Object, MC As Object
Dim S As String
Dim vSrc As Variant, vRes() As Variant
Dim rRes As Range
Dim I As Long, J As Long
'Set upper left corner for results
Set rRes = Range("B1")
'Get data
vSrc = Range("a2", Cells(Rows.Count, "A").End(xlUp))
'Dim Results Array
If IsArray(vSrc) Then
ReDim vRes(1 To UBound(vSrc) + 1, 1 To 3)
Else
ReDim vRes(1 To 2, 1 To 3)
End If
vRes(1, 1) = "Time"
vRes(1, 2) = "User"
vRes(1, 3) = "Inbox File"
Set RE = CreateObject("vbscript.regexp")
With RE
.ignorecase = True
.MultiLine = True
.Pattern = "^.*((?:\d{2}:\d{2}:)\d{2}).*User\s*\[([^]]+).*Inbox/([^]]+)"
If IsArray(vSrc) Then
For I = 1 To UBound(vSrc)
S = vSrc(I, 1)
If .test(S) = True Then
Set MC = .Execute(S)
For J = 0 To 2
vRes(I + 1, J + 1) = MC(0).submatches(J)
Next J
End If
Next I
Else
S = vSrc
If .test(S) = True Then
Set MC = .Execute(S)
For J = 0 To 2
vRes(2, J + 1) = MC(0).submatches(J)
Next J
End If
End If
End With
Set rRes = rRes.Resize(rowsize:=UBound(vRes, 1), columnsize:=UBound(vRes, 2))
rRes.EntireColumn.Clear
rRes = vRes
rRes.EntireColumn.AutoFit
End Sub