按日期同步多组数据

时间:2015-08-27 15:17:46

标签: excel excel-vba vba

我所拥有的是多组带时间戳的数据日志:例如,每5秒就有一个数据点。

它看起来像这样:

1 - 06:31:01 - 0.1
2 - 06:31:06 - 0.4
3 - 06:31:11 - 0.3

第三列只是数据

然而,有时机器会停止记录数据几分钟,这使得很难将它们与时间匹配并排放置,特别是如果这种情况发生了很多时间,而且日志超过了半天!

我正在寻找的是一种算法,以便在必要时使用vba excel同步excel中的日志。

1 个答案:

答案 0 :(得分:0)

在您的VBA IDE中,转到工具菜单并选择参考。选择“Microstoft ActiveX数据对象2.8库。

这假定您的第一个列表位于第1页,第二个列表位于第2页,时间位于A列,值位于B列。它将数据写入表3。

Private Sub LineUpLists()
    Dim ws1 As Excel.Worksheet
    Dim ws2 As Excel.Worksheet
    Dim ws3 As Excel.Worksheet

    Set ws1 = ActiveWorkbook.Sheets("Sheet1")
    Set ws2 = ActiveWorkbook.Sheets("Sheet2")
    Set ws3 = ActiveWorkbook.Sheets("Sheet3")

    Dim rs As New ADODB.Recordset
    Dim lRow As Long

    'Add fields to your recordset for storing data.  You can store sums here.
    With rs
        .Fields.Append "Row", adInteger
        .Fields.Append "Time", adDouble
        .Fields.Append "Value1", adSingle
        .Fields.Append "Value2", adSingle
        .Open
    End With

    'Read the first list from sheet one.
    lRow = 1
    ws1.Activate
    'Loop through the first list and record what is in the columns
    Do While lRow <= ws1.UsedRange.Rows.count

        If ws1.Range("A" & lRow).Value <> "" Then
            rs.AddNew
            rs.Fields("Row").Value = lRow
            rs.Fields("Time").Value = Trim(str(ws1.Range("A" & lRow).Value))
            rs.Fields("Value1").Value = ws1.Range("B" & lRow).Value
            rs.Update
        End If

        lRow = lRow + 1
        ws1.Range("A" & lRow).Activate
    Loop

    'Read the second list from sheet 2
    lRow = 1
    ws2.Activate
    'Loop through the second list and record what is in the columns
    Do While lRow <= ws2.UsedRange.Rows.count

        If ws2.Range("A" & lRow).Value <> "" Then

            'Check if we already recorded this time in the first list
            rs.Filter = ""
            rs.Filter = "Time=" & Trim(str(ws2.Range("A" & lRow).Value))

            If rs.RecordCount = 1 Then
                'If we already have this time, record the second list value
                rs.Fields("Row").Value = lRow
                rs.Fields("Time").Value = Trim(str(ws2.Range("A" & lRow).Value))
                rs.Fields("Value2").Value = ws2.Range("B" & lRow).Value
                rs.Update
            Else
                'If we didn't see this time on the first list, create a new record for it.
                rs.AddNew
                rs.Fields("Row").Value = lRow
                rs.Fields("Time").Value = ws2.Range("A" & lRow).Value
                rs.Fields("Value2").Value = ws2.Range("B" & lRow).Value
                rs.Update
            End If
        End If

        lRow = lRow + 1
        ws2.Range("A" & lRow).Activate
    Loop

    rs.Filter = ""
    rs.Sort = "Time"

    'Switch to sheet 3
    ws3.Activate
    ws3.Range("B1").Value = "Time"
    ws3.Range("B1").Value = "List1"
    ws3.Range("C1").Value = "List2"

    lRow = 2

    'Here we loop through the data we collected and write it out.
    Do While rs.EOF = False
        ws3.Range("A" & lRow).Value = Format(rs.Fields("Time").Value, "hh:mm:ss")
        ws3.Range("B" & lRow).Value = rs.Fields("Value1").Value
        ws3.Range("C" & lRow).Value = rs.Fields("Value2").Value
        lRow = lRow + 1
    rs.MoveNext
    Loop
End Sub