2015-08-27 27 views
1

我有什么是多集,是时间戳数据的日志:有每5秒例如数据点。同步多组数据按日期

看起来是这样的:

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

其中第三列是单纯的数据

但是,有时机器会停止几分钟记录数据,因此很难让他们副作用与时间匹配并排,特别是如果这种情况发生很多时间,那么半天的日志就会发生!

我正在寻找的是一种算法,使用如有必要VBA擅长Excel中的日志进行同步。

+0

详细一点 - 例如,你想要的东西像SQL的结果'OUTER JOIN'(行排列在那里匹配,但在空白不匹配行)? – aucuparia

+0

是1,2,3行号或在列的实际值? – MatthewD

+0

如果我理解正确的话它做什么OUTER JOIN是不是我要找的。至于第一colomn,是那些排数字,MatthewD的代码输出我的预期。 –

回答

0

在你VBA IDE从工具菜单和selecte引用。选择“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