• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Count between Two Cells

fareedexcel

Member
Dear Experts,

I require to check the count interval between the 1st OFF to the Last OFF. After the Last OFF, count should not happen. Expected result attached for your reference.
 

Attachments

  • check-count-between-two-cells-check-count - 1.xlsx
    13.3 KB · Views: 12
fareedexcel
Expected result attached for your reference.
Just modify those '-' and 'OFF' as You need...
 

Attachments

  • check-count-between-two-cells-check-count - 1.xlsb
    16.9 KB · Views: 7
See attached with UDF:
Code:
Function myCounts(rng) As String
ResultStr = ""
rngVals = rng.Value
For c = 2 To UBound(rngVals, 2)
  Select Case True
    Case UCase(Application.Trim(rngVals(1, c - 1))) = "OFF" And UCase(Application.Trim(rngVals(1, c))) <> "OFF": Start = c
    Case UCase(Application.Trim(rngVals(1, c - 1))) <> "OFF" And UCase(Application.Trim(rngVals(1, c))) = "OFF"
      If Start > 0 Then If Len(ResultStr) = 0 Then ResultStr = c - Start Else ResultStr = ResultStr & "-" & c - Start
  End Select
Next c
myCounts = ResultStr
End Function
 

Attachments

  • Chandoo36062check-count-between-two-cells-check-count - 1.xlsm
    21.2 KB · Views: 5
See attached with UDF:
Code:
Function myCounts(rng) As String
ResultStr = ""
rngVals = rng.Value
For c = 2 To UBound(rngVals, 2)
  Select Case True
    Case UCase(Application.Trim(rngVals(1, c - 1))) = "OFF" And UCase(Application.Trim(rngVals(1, c))) <> "OFF": Start = c
    Case UCase(Application.Trim(rngVals(1, c - 1))) <> "OFF" And UCase(Application.Trim(rngVals(1, c))) = "OFF"
      If Start > 0 Then If Len(ResultStr) = 0 Then ResultStr = c - Start Else ResultStr = ResultStr & "-" & c - Start
  End Select
Next c
myCounts = ResultStr
End Function
The Code is working fine. Is there any way to understand the logic. I would like to learn more on automating stuffs in excel.
 
I'm sorry, but no; explaining the logic in full takes 10 to 20 or even 30 times as long as writing the code.
 
I'm sorry, but no; explaining the logic in full takes 10 to 20 or even 30 times as long as writing the code.
Thanks for your support Expert. I need one more modification from the same file, for eg: in the pattern if any of them has more than value 7 (6-8-6, 12-6-8,6-6-6-8) then those rows should come in another sheet. Is it possible with Macros instead of doing with formula.
 
Button at cell AP1 of Check sheet in attached, which calls the code below.
Uses the same user defined function as before.
Code:
Sub blah()
Limit = 7
For Each rw In Sheets("Check").Range("I4:AM19").Rows
  x = myCounts(rw)
  If Len(x) > 0 Then
    y = Split(x, "-")
    GreaterThanLimit = False
    For Each Z In y
      If CLng(Z) > Limit Then
        GreaterThanLimit = True
        Exit For
      End If
    Next Z
    If GreaterThanLimit Then Set Destn = Sheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Offset(1) Else Set Destn = rw.Cells(1).Offset(, 33)
    With Destn
      .NumberFormat = "@"
      .Value = x
    End With
  End If
Next rw
End Sub
 

Attachments

  • Chandoo36062check-count-between-two-cells-check-count - 2.xlsm
    27.2 KB · Views: 2
Last edited:
Button at cell AP1 of Check sheet in attached, which calls the code below.
Uses the same user defined function as before.
Code:
Sub blah()
Limit = 7
For Each rw In Sheets("Check").Range("I4:AM19").Rows
  x = myCounts(rw)
  If Len(x) > 0 Then
    y = Split(x, "-")
    GreaterThanLimit = False
    For Each Z In y
      If CLng(Z) > Limit Then
        GreaterThanLimit = True
        Exit For
      End If
    Next Z
    If GreaterThanLimit Then Set Destn = Sheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Offset(1) Else Set Destn = rw.Cells(1).Offset(, 33)
    With Destn
      .NumberFormat = "@"
      .Value = x
    End With
  End If
Next rw
End Sub
After the pattern is captured in the 1st sheet. Above 7 should get captured in the second sheet. Please help on the same. Expected result attached. Thanks for the help Experts.
 

Attachments

  • Chandoo36062check-count-between-two-cells-check-count - 1 Exp Result.xlsm
    21.3 KB · Views: 4
Back
Top