按date同步多组数据

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

它看起来像这样:

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

第三列只是数据

然而,有时机器会停止logging数据几分钟,这使得它们很难与时间匹配并行,特别是如果这种情况发生在半天以上的日志上,这种情况尤其如此。

我正在寻找的是一个algorithm来同步Excel中的日志,如果有必要使用VBA的Excel。

在你的VBA IDE进入工具菜单并select参考。 select“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