如何从单元格中的字符串中间提取文本

时间:2015-09-25 16:42:25

标签: excel-vba vba excel

所以我将日志从服务器导出到.csv文件。我将其转换为.xlsm文件以获取内容。我想拉出从给定单元格登录的用户名。我想要提取的所有单元都具有以下格式:

An account was successfully logged on.

Subject:
    Security ID:        NULL SID
    Account Name:       -
    Account Domain:     -
    Logon ID:       0x0

Logon Type:         3

New Logon:
    Security ID:        ZZZ\username
    Account Name:       username
-----------------------------

我想从此单元格中提取用户名并将其放入相邻的单元格中,以创建所有登录的数据透视表。

如何只提取用户名?

1 个答案:

答案 0 :(得分:0)

试试这个:

Option Explicit

Public Sub getUserName()

    Const COL   As Long = 1
    Const AN    As String = "Account Name:"

    Dim ur As Range, v As Variant, i As Long, x As String

    Set ur = ActiveSheet.UsedRange

    v = ur.Columns(COL).Resize(ur.Row + ur.Rows.Count - 1, 2)
    For i = 1 To ActiveSheet.UsedRange.Rows.Count
        x = Right(v(i, 1), Len(v(i, 1)) - (InStrRev(v(i, 1), AN) - 1))
        x = Trim(Mid(x, Len(AN) + 1))
        v(i, 2) = Left(x, InStr(x, vbLf) - 1)
    Next
    ur.Columns(COL).Resize(ur.Row + ur.Rows.Count - 1, 2) = v
End Sub

目前设置从A列提取到B列