这是对here发现的问题的跟进。
等级' Eh'提供的解决方案。培根假设每个十进制代码对应于特定状态,但情况并非如此。必须首先将十进制代码转换为二进制,并且每个位对应于特定描述。因此,例如,0000000000000001将仅对应于一个状态" Idle Cutout Active"和0000000001000001将对应于两种状态"高冷却剂温度" 和" Idle Cutout Active"。
我设计了一种方法将16位字符串拆分为每个位并填充一系列列,以便我的数据如下所示:
注意:数据继续远远超出上图中可以看到的行;直到第8651行。
有没有办法可以修改此解决方案来计算1s列D到S,而不是查看B列中的十进制代码?我想这会导致使用大量的列,所以也许有一种更好的方式来做我想要完成的事情。重申一下,我想要做的是找到五个或更多个连续1的开始和结束时间,时间戳在每个2分钟内,并且相应的描述符显示有开始和结束时间戳,以便我可以实现上面显示的数据的可读格式更易读。它看起来像是:
那么,有没有办法修改当前的解决方案,还是应该考虑一个全新的解决方案?如果是后者,你会推荐什么?
到实验:
我已经整理了一份带有等级' Eh' Bacon的解决方案已经实施,并且是我正在使用的数据示例。可以从here下载工作簿。
谢谢,
丹
编辑1:
我已经修改了Tim Williams发布的代码以满足我的需求,并且还增加了一些功能,使其更好地适用于我正在做的事情。 Google云端硬盘上的文件已完整更新了代码,但总结一下,我对Tim的代码进行了一些细微的修改,以便它符合我的数据,然后编写了两个额外的子代码。首先是替换所有各种" Flag#:XX"使用引擎状态描述符运行代码时出现的值,另一个是按开始时间戳对值进行排序。以下是我的代码看起来像我所做的改动:
Public Type flag
tStart As Variant 'flag start time
tLast As Variant 'last event time
Count As Long 'number of 1's
End Type
Sub Report()
'Type to track data for each of the bit positions (event types)
Dim flags() As flag
Dim c As Range, nF As Long, i As Long, v, f, t
Dim shtReport As Worksheet, bContinue As Boolean
Set shtReport = ThisWorkbook.Sheets("Technician Report Summary")
Set c = Sheets("RAW DATA").Range("C2")
nF = Len(c.Value)
ReDim flags(1 To nF)
bContinue = True
Do
v = c.Value 'bitstring
t = c.Offset(0, -2).Value 'timestamp
If Len(v) = 0 Then
'No more data, so insert a "dummy" value to trigger
' writing out any current "events"
v = String(nF, "0")
bContinue = False
End If
'loop over each "bit" in the string
For i = 1 To nF
f = Mid(v, i, 1)
If f = "1" Then
'first instance of this flag ?
If flags(i).Count = 0 Then
flags(i).tStart = t 'event begins - set start time
Else
flags(i).tLast = t 'event continues - set "last" time
End If
flags(i).Count = flags(i).Count + 1
End If
If f = "0" Then
'end of a run of 5 or more 1's ?
If flags(i).Count >= 5 Then
'write event to Report sheet
With shtReport.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)
.Value = "Flag#:" & i
.Offset(0, 1).Value = flags(i).tStart
.Offset(0, 2).Value = flags(i).tLast
End With
End If
flags(i).Count = 0
End If
Next i
Set c = c.Offset(1, 0)
Loop While bContinue
Call Sort
Call FindAndReplace
End Sub
我认为它现在正在寻找我正在寻找的东西。我会进一步验证,但就目前而言,我认为我找到了一个有效的解决方案。
答案 0 :(得分:2)
您可以尝试这样的事情:
'Type to track data for each of the bit positions (event types)
Public Type flag
tStart As Variant 'flag start time
tLast As Variant 'last event time
Count As Long 'number of 1's
End Type
Sub Report()
Dim flags() As flag
Dim c As Range, nF As Long, i As Long, v, f, t
Dim shtReport As Worksheet, bContinue As Boolean
Set shtReport = ThisWorkbook.Sheets("Report")
Set c = Sheet1.Range("B2")
nF = Len(c.Value)
ReDim flags(1 To nF)
bContinue = True
Do
v = c.Value 'bitstring
t = c.Offset(0, -1).Value 'timestamp
If Len(v) = 0 Then
'No more data, so insert a "dummy" value to trigger
' writing out any current "events"
v = String(nF, "0")
bContinue = False
End If
'loop over each "bit" in the string
For i = 1 To nF
f = Mid(v, i, 1)
If f = "1" Then
'first instance of this flag ?
If flags(i).Count = 0 Then
flags(i).tStart = t 'event begins - set start time
Else
flags(i).tLast = t 'event continues - set "last" time
End If
flags(i).Count = flags(i).Count + 1
End If
If f = "0" Then
'end of a run of 5 or more 1's ?
If flags(i).Count >= 5 Then
'write event to Report sheet
With shtReport.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)
.Value = "Flag#:" & i
.Offset(0, 1).Value = flags(i).tStart
.Offset(0, 2).Value = flags(i).tLast
End With
End If
flags(i).Count = 0
End If
Next i
Set c = c.Offset(1, 0)
Loop While bContinue
End Sub
编辑:修正了一些拼写错误并说明了如果有事件发生在数据末尾会发生什么"正在进行"