简体   繁体   中英

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. 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).
  • of a certain format (mm:ss)
  • between 2 cells filled with strings.

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.
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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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