excel宏来格式化数据

时间:2011-05-02 13:41:06

标签: string excel formatting

我的数据格式如下,

Date         User            Time         Status     Domain
2011Apr18   ID:user1)   10:26:55.078>   loggedinto  A
2011Apr18   ID:user1)   14:09:31.010>   loggedout   A
2011Apr18   ID:user1)   14:10:37.473>   loggedinto  A
2011Apr18   ID:user1)   15:59:55.899>   loggedinto  A
2011Apr18   ID:user1)   16:22:05.335>   loggedout   A
2011Apr18   ID:user2)   16:59:00.769>   loggedinto  A
2011Apr18   ID:user2)   17:14:52.169>   loggedout   A
2011Apr19   ID:user1)   10:05:44.102>   loggedinto  A
2011Apr19   ID:user3)   11:54:27.713>   loggedinto  C

我需要使用excel宏以下格式获得上述数据。再次基于域,国家/地区需要映射为(A-&gt; India,B-&gt; China)...请帮助< / p>

Date            user      Logged into      Logged out    Domain   Country
2011Apr18   ID:user1)   15:59:55.899>   16:22:05.335>        A    India
2011Apr18   ID:user1)   16:22:05.335>   17:14:52.169>        A    India
2011Apr18   ID:user2)   16:59:00.769>   10:05:44.102>        A    India
2011Apr18   ID:user2)   17:14:52.169>   15:59:55.899>        A    India
2011Apr19   ID:user1)   10:05:44.102>   17:14:52.169>        B    China

提前致谢

2 个答案:

答案 0 :(得分:1)

我在这里写了一个小脚本给你。启动ReFormat()以创建新表。它读取活动工作表中的当前数据并将其放入数组中。然后我从G1开始在同一张表中创建新表。

请给我一个反馈,这是一些工作。 (如果它适合你接受答案就好了。)

Option Explicit

Dim DataArray() As String
Dim lngRow As Long, lngLastRow As Long
Dim intColumn As Integer

Sub ReFormat()

    Dim ResultTable As Range
    Dim CurrentResultRow As Long
    Dim i As Long

    FillSourceArray

    Set ResultTable = ActiveSheet.Range("G1")
    CurrentResultRow = 0

    ResultTable.Offset(CurrentResultRow, 0).Value = "Date"
    ResultTable.Offset(CurrentResultRow, 1).Value = "user"
    ResultTable.Offset(CurrentResultRow, 2).Value = "Logged into"
    ResultTable.Offset(CurrentResultRow, 3).Value = "Logged out"
    ResultTable.Offset(CurrentResultRow, 4).Value = "Domain"
    ResultTable.Offset(CurrentResultRow, 5).Value = "Country"

    CurrentResultRow = CurrentResultRow + 1

    For i = 1 To lngLastRow - 1
        If (DataArray(i, 3) = "loggedinto") Then
            ResultTable.Offset(CurrentResultRow, 0).Value = DataArray(i, 0)
            ResultTable.Offset(CurrentResultRow, 1).Value = DataArray(i, 1)
            ResultTable.Offset(CurrentResultRow, 2).Value = DataArray(i, 2)
            ResultTable.Offset(CurrentResultRow, 3).Value = SearchLogOut(DataArray(i, 1), i + 1)
            ResultTable.Offset(CurrentResultRow, 4).Value = DataArray(i, 4)
            ResultTable.Offset(CurrentResultRow, 5).Value = SearchCountry(DataArray(i, 4))
            CurrentResultRow = CurrentResultRow + 1
        End If

    Next i

End Sub

Function SearchLogOut(user As String, Start As Integer) As String
    Dim i As Long

    For i = Start To lngLastRow - 1
        If ((DataArray(i, 1) = user) And (DataArray(i, 3) = "loggedout")) Then
            SearchLogOut = DataArray(i, 2)
            Exit For
        ElseIf ((DataArray(i, 1) = user) And (DataArray(i, 3) = "loggedinto")) Then
            SearchLogOut = ""
            Exit For
        End If
    Next i

End Function

Function SearchCountry(Country As String) As String
    Select Case Country
        Case "A"
            SearchCountry = "India"
        Case "B"
            SearchCountry = "China"
    End Select

End Function

Sub FillSourceArray()

    'Read the Source Data
    lngLastRow = Cells(Rows.Count, 1).End(xlUp).Row
    ReDim DataArray(lngLastRow - 1, 4)

    For lngRow = 1 To lngLastRow
      For intColumn = 1 To 5
        DataArray(lngRow - 1, intColumn - 1) = Cells(lngRow, intColumn)
      Next intColumn
    Next lngRow

End Sub

答案 1 :(得分:0)

对于A - &gt;的简单映射印度和B - &gt;中国,请看VLOOKUP功能。这是一个简单的例子:a8到b11是

a China
b India
c Russia
d Brazil

如果你在a2中有一个“a”,那么这个vlookup函数会做你想要的:

=VLOOKUP(A2,$A$8:$B$11,2)

其中a2是查找值,$ a $ 8:$ b $ 11是表,2表示你想返回b列(第二列)。