将数据转换为时间轴

时间:2018-06-24 19:35:07

标签: vba excel-vba for-loop find excel

我正在尝试转换当前如下所示的数据:

![enter image description here] 1

我希望将其转换为针对每个用户的时间轴,这些时间线是关于哪个进程首先发生以及何时发生的时间表。如: enter image description here

我试图将find函数与在每个用户中运行的循环一起使用,然后将时间与每个相应用户相邻。但是我无法弄清楚如何使find函数在找到第一个事件之后又要找到最小时间,然后再找到第二个最小时间等来多次运行...

我愿意接受有关VBA的其他建议

1 个答案:

答案 0 :(得分:0)

这绝非完美,但它为您提供了基础。

逻辑是:

  1. 在阅读每一列时,使用集合来存储数据, 建立与每个用户相关的数据列表。
  2. 为每个用户创建一个工作表,然后将用户集合中的内容写到相应的工作表中。
  3. 使用Excel的排序功能将列表排序。
  4. 将排序后的列表移至“最终结果”表。

代码如下(请注意,该代码当前读取所有列,但只写出UserA。我将留给您以扩展代码以覆盖其他用户):

Option Explicit

Sub TransferTimeCodes()
    Dim UserA As New Collection
    Dim UserB As New Collection
    Dim UserC As New Collection
    Dim sh As New Worksheet
    Dim rw As Range
    Dim ColCount, RowCount As Integer
    Dim Msg As String
    Dim WrdArray() As String


    Set sh = Sheets("Timesheet")
    RowCount = 0
    ColCount = 1

    'Loop through the Process A User column and build up list of timecodes
    ' for users who used Process A
    For Each rw In sh.Rows
        'Check for User A
        If sh.Cells(rw.Row, ColCount).Value = "A" Then
            'Timecode is one cell to the right
            ' Join the two pieces of data together to ensure alignment
            'Note the "A" is fixed because it is Process A
            Msg = "A," & sh.Cells(rw.Row, ColCount).Offset(0, 1).Value
            UserA.Add Msg
        End If
        'Check for User B
        If sh.Cells(rw.Row, ColCount).Value = "B" Then
            Msg = "A," & sh.Cells(rw.Row, ColCount).Offset(0, 1).Value
            UserB.Add Msg
        End If
        'Check for User C
        If sh.Cells(rw.Row, ColCount).Value = "C" Then
            Msg = "A," & sh.Cells(rw.Row, ColCount).Offset(0, 1).Value
            UserC.Add Msg
        End If
        'Check for the end of the column
        If sh.Cells(rw.Row, ColCount).Value = "" Then
            Exit For
        End If
        RowCount = RowCount + 1
    Next rw

    RowCount = 0
    ColCount = 3

    'Loop through the Process B User column and build up list of timecodes
    ' for users who used Process B
    For Each rw In sh.Rows
        If sh.Cells(rw.Row, ColCount).Value = "A" Then
            Msg = "A," & sh.Cells(rw.Row, ColCount).Offset(0, 1).Value
            UserA.Add Msg
        End If
        If sh.Cells(rw.Row, ColCount).Value = "B" Then
            Msg = "A," & sh.Cells(rw.Row, ColCount).Offset(0, 1).Value
            UserB.Add Msg
        End If
        If sh.Cells(rw.Row, ColCount).Value = "C" Then
            Msg = "A," & sh.Cells(rw.Row, ColCount).Offset(0, 1).Value
            UserC.Add Msg
        End If
        If sh.Cells(rw.Row, ColCount).Value = "" Then
            Exit For
        End If
        RowCount = RowCount + 1
    Next rw

    RowCount = 0
    ColCount = 5

    'Loop through the Process C User column and build up list of timecodes
    ' for users who used Process C
    For Each rw In sh.Rows
        If sh.Cells(rw.Row, ColCount).Value = "A" Then
            Msg = "A," & sh.Cells(rw.Row, ColCount).Offset(0, 1).Value
            UserA.Add Msg
        End If
        If sh.Cells(rw.Row, ColCount).Value = "B" Then
            Msg = "A," & sh.Cells(rw.Row, ColCount).Offset(0, 1).Value
            UserB.Add Msg
        End If
        If sh.Cells(rw.Row, ColCount).Value = "C" Then
            Msg = "A," & sh.Cells(rw.Row, ColCount).Offset(0, 1).Value
            UserC.Add Msg
        End If
        If sh.Cells(rw.Row, ColCount).Value = "" Then
            Exit For
        End If
        RowCount = RowCount + 1
    Next rw

    Sheets.Add(After:=Sheets(Sheets.Count)).Name = "UserA"
    Set sh = Sheets("UserA")
    For RowCount = 0 To UserA.Count - 1
        WrdArray() = Split(UserA(RowCount + 1), ",")
        sh.Range("A1").Offset(RowCount, 0).Value = WrdArray(0)
        sh.Range("A1").Offset(RowCount, 1).Value = WrdArray(1)
    Next RowCount
    sh.UsedRange.Sort key1:=Range("B1"), order1:=xlAscending, Header:=xlNo
    sh.UsedRange.Columns(2).Copy
    Worksheets("FinalResults").Range("B2").PasteSpecial Transpose:=True

End Sub