如何在Excel中的列中logging从0到1的转换?
我需要logging在一个单一列在Excel中从下降状态(0)到上升状态(1)的转换次数,我想知道在Excel中是否有这样的逻辑可用,或者我是否可以编程这种types在VBA中的逻辑。 我不是很了解VBA,所以如果有任何链接可用,那就太好了。 谢谢你们的帮助!
考虑使用Sumproduct()工作表函数:
Sub Foo() No_of_rows = Cells(Rows.Count, 1).End(xlUp).Row temp = 0 For i = 1 To No_of_rows If Cells(i, 1) = 0 And Cells(i + 1, 1) = 1 Then temp = temp + 1 End If Next Cells(1, 2) = temp End Sub
结果将在B1
以下嵌套的Excelexpression式
=IF(AND(OFFSET(INDIRECT(ADDRESS(ROW(), COLUMN())),0,-1)=0,OFFSET(INDIRECT(ADDRESS(ROW(), COLUMN())),1,-1)=1), 1, 0)
如果单元格的值为0,那么将单元格值设置为1,并将单元格的值设置为1。
0 0 0 0 0 1 <-- Transition 1 0 1 0 1 0 0 0 0 1 <-- Transition 1 0 0 0 0 0
我必须同意,选定的答案比这个要干净得多,但如果标记过渡是重要的,那么这是一个select。
以下应该工作。
将代码粘贴到工作簿中的VBE中
Dim lngCount As Long Dim rngTemp As Range Dim rngCell As Range 'Define Range on spreadsheet, Range A1:A13 in this example Set rngTemp = Range(Cells(1, 1), Cells(13, 1)) 'Clear Counter lngCount = 0 'Loop through range and count the changes while ignoring the first row For Each rngCell In rngTemp If rngCell.Row <> 1 Then If rngCell.Value = 1 And rngCell.Offset(-1, 0).Value = 0 Then lngCount = lngCount + 1 End If End If Next rngCell MsgBox "There are " & lngCount & " transitions"