Is there a process in Excel that can automate reading through a column containing TRUE/FALSE values and can add numbers incrementally in a new column?

huangapple go评论73阅读模式
英文:

Is there a process in Excel that can automate reading through a column containing TRUE/FALSE values and can add numbers incrementally in a new column?

问题

我有一个非常大的电子表格,包含超过50,000行。它们是来自摄像陷阱的事件记录。我有一列名为“NewEvent”的列,其中只包含TRUE或FALSE的值,这些值是基于摄像陷阱是否作为新事件触发而设置的。然而,现在我需要按事件发生的顺序对事件进行编号,并将这些值记录在一个新的列中。例如,第一个TRUE将在新列中标记为事件1,其后的所有FALSE值仍然属于事件1,直到再次出现TRUE,然后它将成为事件2,依此类推。我开始手动操作,但意识到这将花费我相当多的时间。是否有办法在Excel中使用VBA或宏等自动化此过程?请参见下面的示例:

NewEvent EventNum
TRUE 1
FALSE 1
FALSE 1
FALSE 1
TRUE 2
FALSE 2
FALSE 2
TRUE 3
FALSE 3
FALSE 3
FALSE 3
FALSE 3

我尝试在宏的代码编辑器中进行实验,但我对此非常陌生,发现它对我来说有点复杂。非常感谢!

英文:

I have a very large spreadsheet containing over 50,000 rows. They are records of events from a camera trap. I have a column called "NewEvent" which contains only TRUE or FALSE values, which was based on whether the camera trap was triggered as a new event. However now I need to number the events in the order that they happened and record these values in a new column. For example, the very first TRUE would be marked as event 1 in the new column, and all FALSE values following it would also still be part of event 1, until there is another TRUE which would then be event 2 and so on. I started doing this manually but realized it will take me a considerable amount of time. Is there any way to automate this using VBA or macro etc in Excel? Please see the example below:

NewEvent EventNum
TRUE 1
FALSE 1
FALSE 1
FALSE 1
TRUE 2
FALSE 2
FALSE 2
TRUE 3
FALSE 3
FALSE 3
FALSE 3
FALSE 3

I tried experimenting with the code editor in Macro but I am very new to this and found it a little complex for me. Thank you so much!

答案1

得分: 6

更新:这个帖子变成了一种代码竞赛。最短的答案似乎是:

=N(B1)+A2

原始答案:

使用混合范围引用的 COUNTIFS。如果你有数以万计的行,可能会导致工作表变慢!

=COUNTIFS(A$2:A2,TRUE)

另一个性能更好的选项:

=IF(A2,SUM(B1,1),B1)

还有另一种选项(感谢Mayukh Bhattacharya):

=IF(A2,N(B1)+1,B1)

Is there a process in Excel that can automate reading through a column containing TRUE/FALSE values and can add numbers incrementally in a new column?

英文:

UPDATE: This thread turned into a bit of Code Golf. The shortest answer appears to be:

=N(B1)+A2

Is there a process in Excel that can automate reading through a column containing TRUE/FALSE values and can add numbers incrementally in a new column?


ORIGINAL ANSWER:

Using COUNTIFS with a mixed range reference. May bog down the worksheet if you have tens of thousands of rows!

=COUNTIFS(A$2:A2,TRUE)

Is there a process in Excel that can automate reading through a column containing TRUE/FALSE values and can add numbers incrementally in a new column?

Another (more performant) option:

=IF(A2,SUM(B1,1),B1)

Is there a process in Excel that can automate reading through a column containing TRUE/FALSE values and can add numbers incrementally in a new column?

Yet another option (credit to Mayukh Bhattacharya)

=IF(A2,N(B1)+1,B1)

Is there a process in Excel that can automate reading through a column containing TRUE/FALSE values and can add numbers incrementally in a new column?

答案2

得分: 5

除了**BigBen**先生提供的解决方案外,您可以使用<kbd>SCAN( )</kbd>来溢出。


• 在单元格B2中使用的公式

=SCAN(0,--A2:A13,LAMBDA(x,y,IF(y,x+1,x)))

编辑:用于Spill Array!


• 在单元格B1中使用的公式

=SCAN(0,--A2:A13,LAMBDA(x,y,(y+N(x))))

另一种替代方法。不使用<kbd>IF( )</kbd>,仅使用<kbd>N( )</kbd>


• 在单元格B1中使用的公式

=N(A2)+N(B1)

当然,这个公式需要向下填充!!


上述公式可以根据@BigBen先生的建议更短。

=A2+N(B1)

强制性注意事项:上述显示的公式都假定范围以TRUE开头,如果不是,则可以使用以下方法:


• 在单元格B1中使用的公式

=(A2+N(B1))+1
英文:

Other than the solution provided by BigBen Sir, you can use <kbd>SCAN( )</kbd> to spill.


Is there a process in Excel that can automate reading through a column containing TRUE/FALSE values and can add numbers incrementally in a new column?


• Formula used in cell B2

=SCAN(0,--A2:A13,LAMBDA(x,y,IF(y,x+1,x)))

Edit: For the Spill Array!

Is there a process in Excel that can automate reading through a column containing TRUE/FALSE values and can add numbers incrementally in a new column?


• Formula used in cell B1

=SCAN(0,--A2:A13,LAMBDA(x,y,(y+N(x))))

Another alternative approach. Without <kbd>IF( )</kbd> and using only <kbd>N( )</kbd>


Is there a process in Excel that can automate reading through a column containing TRUE/FALSE values and can add numbers incrementally in a new column?


• Formula used in cell B1

=N(A2)+N(B1)

OfCourse the formula needs to fill down!!


The above formula can be made more shorter as proposed by @BigBen Sir.

=A2+N(B1)

Is there a process in Excel that can automate reading through a column containing TRUE/FALSE values and can add numbers incrementally in a new column?


A mandatory caveat : Formulas shown above all assumes, that the range starts with TRUE, if it isn't then one could use as below:

Is there a process in Excel that can automate reading through a column containing TRUE/FALSE values and can add numbers incrementally in a new column?


• Formula used in cell B1

=(A2+N(B1))+1

答案3

得分: 3

For fun:

Is there a process in Excel that can automate reading through a column containing TRUE/FALSE values and can add numbers incrementally in a new column?

B2 中的公式:

=MAP(A2:A13,LAMBDA(x,SUM(--A2:x)))

或者,使用 SCAN()

=SCAN(0,A2:A13,LAMBDA(x,y,x+y))

英文:

For fun:

Is there a process in Excel that can automate reading through a column containing TRUE/FALSE values and can add numbers incrementally in a new column?

Formula in B2:

=MAP(A2:A13,LAMBDA(x,SUM(--A2:x)))

Or, with SCAN():

=SCAN(0,A2:A13,LAMBDA(x,y,x+y))

答案4

得分: 2

请看这个老式的技巧:

假设您在列“A”中有您的“TRUE”/“FALSE”值,并且您想要在列“B”中进行计数,那么您可以从1开始填充列“B”(因为您始终从“TRUE”开始),然后列“B”的以下单元格使用以下公式:

=IF(A2=TRUE,B1+1,B1)

这将检查“A”列中的值。如果为TRUE,则进行递增(B1+1),否则使用正常值(B1)。

结果如下所示:

Is there a process in Excel that can automate reading through a column containing TRUE/FALSE values and can add numbers incrementally in a new column?

英文:

What about this old-fashioned trick:

Imagine you have your "TRUE"/"FALSE" values in column "A" and you want your counter in column "B", then you start column "B" with 1 (as you always start with a TRUE"), and the following cell in column "B" gets following formula:

=IF(A2=TRUE,B1+1,B1)

This checks the value in the "A" column. In case of TRUE, an increase (B1+1) is done, otherwise the normal value (B1) is used.

The result looks as follows:

Is there a process in Excel that can automate reading through a column containing TRUE/FALSE values and can add numbers incrementally in a new column?

huangapple
  • 本文由 发表于 2023年7月12日 21:36:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76671196.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定