简体   繁体   English

Excel(需要VBA吗?)选择一行中具有特定格式并已填充的所有单元格,然后对它们进行计数。 在公式中使用该计数

[英]Excel (VBA needed?) Select all Cells in a row that have a certain format and are filled, then count them. Use that count in a formula

Analysing some data I got for my company, I need to count cells, so I can work in a formula with that count. 分析我为公司获得的一些数据,我需要对单元进行计数,因此我可以使用该计数来计算公式。
CurrentRegion doesn't work. CurrentRegion不起作用。 Here's what criteria need to be met. 这是需要满足的条件。

The cells counted must be: 计数的单元格必须是:

  • of a certain row (there are other filled cells nearby, which is why CurrentRegion doesn't work). 某一行(附近还有其他填充的单元格,这就是CurrentRegion不起作用的原因)。
  • of a certain format (mm:ss) 特定格式(mm:ss)
  • between 2 cells filled with strings. 2个单元之间充满字符串。

This data is collected automatically, so the coordinates of the fields that are filled with strings are not set. 该数据是自动收集的,因此不会设置用字符串填充的字段的坐标。

The (set) row looks like this: (设置)行如下所示:

Description  
Description  
...  
Time  
Time  
Time            <- This is the data that I want to count.  
Time  
Time  
...  
Description  
Time  
Time  
...  

If it was just one of these I might get it, but for me being relatively new to VBA, this is very hard. 如果只是其中之一,我可能会明白,但是对我来说,对于VBA来说还比较陌生,这很难。
I appreciate every hint. 我感谢所有提示。 I don't expect anyone to write the code for me 我不希望有人为我编写代码

Only to start ...: 才开始...:

Dim e As Integer

e = 0
For i = 1 To 9999
    If Cells(i, 1).Value = "" Then Exit For
    If IsNumeric(Cells(i, 1).Value) Then
        e = e + 1
        ' Debug.Print Format(Cells(i, 1).Value, "h:m:s")
    Else
        If e <> 0 Then Debug.Print "Count = " & e
        e = 0
    End If
Next

the macro print for every section the number of consecutive time values. 宏为每个部分打印连续时间值的数量。
The commented Print show the time formatted. 带注释的打印显示了格式化的时间。
You need to add the code to continue ... 您需要添加代码才能继续...
If you want also the Range of rows, modify a little the code: 如果还需要行范围,请修改一些代码:

Dim e, i, Addr1 As Integer

e = 0
For i = 1 To 9999
    If Cells(i, 1).Value = "" Then Exit For
    If IsNumeric(Cells(i, 1).Value) Then
        If e = 0 Then Addr1 = i
        e = e + 1
        ' Debug.Print Format(Cells(i, 1).Value, "h:m:s")
    Else
        If e <> 0 Then Debug.Print "Count = " & e & " - Range of Rows: " & Addr1 & " -> " & i - 1
        e = 0
    End If
Next

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM