Count consecutive till change of sign ignore "1's"

Kishan

Well-known Member
Joined
Mar 15, 2011
Messages
1,648
Office Version
  1. 2010
Platform
  1. Windows
Using Excel 2000</SPAN></SPAN>

Hi,</SPAN></SPAN>

Data located in columns C:P in cells C6:P44</SPAN></SPAN>
X & 2 counts result is shown in the columns R:Z in the cells R6:Z44</SPAN></SPAN>
Note: Colours are filled just to show example clearer</SPAN></SPAN>

I need to count X's AND 2's consecutive till change of Sign X from 2 Or 2 From X (Ignore 1's) </SPAN></SPAN>

Example for row 6</SPAN></SPAN>
X = 1 time, result in R6 = 1 </SPAN></SPAN>
2 = 2 time, result in S6 = 2 </SPAN></SPAN>
X = 2 time, result in T6 = 2 </SPAN></SPAN>
2 = 1 time, result in U6 = 1</SPAN></SPAN>
X = 1 time, result in V6 = 1 </SPAN></SPAN>
2 = 1 time, result in W6 = 1</SPAN></SPAN>

Count method will be same for each line</SPAN></SPAN>

Result data example</SPAN></SPAN>


Book1
ABCDEFGHIJKLMNOPQRSTUVWXYZAA
1
2
3
4
5P1P2P3P4P5P6P7P8P9P10P11P12P13P14X2X2X2X2X
6X1212XX112X112122111
71X2221XX22XX1113222
81X11XX22XX11XX324
91X1221X11X12211222
10X1X1X11X111XX16
11XXX1111X1X212X521
122X1112XXX1112111131
13X1XX1111111X114
141XX12121112X2X215
15X2X11X1111X11X114
16111111XXX1112X311
171212X112X2111X211111
181XX112XX1X11X1214
191XX11211221X11231
20111X1111X111112
21X12X1112X112XX1111112
22111X1XX1X111114
2311111XX211111222
2411111111112XX112
2512211111111X1121
2611X1111111112111
27XXX111X112X111411
28X211211111XX1X123
291X221112XX111X133
3021XX11XX1X111115
311X12211112111X131
3211X122211X1X1X133
3311111111111111
34X11111212XX1XX124
351211X111X2X1XX1213
36X1111X1211X1XX213
372X111XX11X11X115
381X2X1X1111121X11211
39X1XX1X11X1221152
401XX12X11X1X1212131
411X11X1XX21111X411
42121111X1XX1X22142
43X111111121221X131
442XX11X11XXXX1X18
45
46
47
Hoja1


Thank you in advance</SPAN></SPAN>

Regards,</SPAN>
Kishan</SPAN>
 
Last edited:

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Here is some VBA that does what you want...
-

The VBA
- builds a string of counts for values found in columns C:P, starting at row 6
- splits that string into columns R onwards
- the string starts with a comma if the first value in range is "2" (eg row 12)
- below are the strings for rows 6 to 12
Row 6 1,2,2,1,1,1
Row 7 1,3,2,2,2
Row 8 3,2,4
Row 9 1,2,2,2
Row 10 6
Row 11 5,2,1
Row 12 ,1,1,1,3,1


VBA goes in sheet module
- right-click on sheet tab \ view code \ copy and paste into pane on right
Code:
Option Explicit
    Dim countX As Integer, count2 As Integer, myStr As String

Sub Count2orX()
    Dim r As Long, celA As Range, celB As Range, rng As Range
    Dim countArray As Variant
   
For r = 6 To Range("C" & Rows.Count).End(xlUp).Row
'build delimited string of counts for this row
    ResetAll
    Set rng = Range("C" & r).Resize(, 14)       '14 columns in range
    For Each celA In rng
        Select Case celA.Value
            Case "X"
                    countX = countX + 1
                    If count2 > 0 Then Reset2
            Case 2
                    count2 = count2 + 1
                    If countX > 0 Then ResetX
        End Select
    Next celA
    If count2 > 0 Then Reset2 Else ResetX
'place values in cells
    If myStr <> "0" Then
        countArray = Split(StringOfCounts(rng), ",")
        Set celB = Range("R" & r).Resize(, UBound(countArray) + 1)
        celB.Value = countArray
        celB.Value = celB.Value                 'avoids numbers being formatted as text
    End If
Next r
End Sub

Function StringOfCounts(aRange As Range) As String
'this function inserts an extra delimiter at beginning of string if first found value is "2"
    Dim colX As Long, col2 As Long
    col2 = 9999: colX = 9999                'large default value simplifies IF test
    On Error Resume Next
    With WorksheetFunction
        col2 = .Match(2, aRange, 0)
        colX = .Match("X", aRange, 0)
    End With
    If col2 < colX Then myStr = "," & myStr
Debug.Print "Row " & aRange.Row, myStr                   'see note
    StringOfCounts = myStr
End Function

Private Sub ResetX()
    If myStr = "" Then myStr = countX Else myStr = myStr & "," & countX
    countX = 0
End Sub
Private Sub Reset2()
    If myStr = "" Then myStr = count2 Else myStr = myStr & "," & count2
    count2 = 0
End Sub
Private Sub ResetAll()
    myStr = ""
    countX = 0
    count2 = 0
End Sub

see result of this line in VBA immediate window which is viewed with {CTRL} G
Code:
Debug.Print "Row " & aRange.Row, myStr
delete the line after testing
 
Upvote 0
Here is some VBA that does what you want...
-

The VBA
- builds a string of counts for values found in columns C:P, starting at row 6
- splits that string into columns R onwards
- the string starts with a comma if the first value in range is "2" (eg row 12)
- below are the strings for rows 6 to 12
Row 6 1,2,2,1,1,1
Row 7 1,3,2,2,2
Row 8 3,2,4
Row 9 1,2,2,2
Row 10 6
Row 11 5,2,1
Row 12 ,1,1,1,3,1


VBA goes in sheet module
- right-click on sheet tab \ view code \ copy and paste into pane on right
Code:
Option Explicit
    Dim countX As Integer, count2 As Integer, myStr As String

Sub Count2orX()
    
End Sub

see result of this line in VBA immediate window which is viewed with {CTRL} G
Code:
Debug.Print "Row " & aRange.Row, myStr
delete the line after testing
WOW!! Yongle, it worked like magic tried inclusive with my original data 9000+ rows it is so speedy. Also saw result of this line in VBA immediate window which is viewed with {CTRL} G, is perfect</SPAN></SPAN>

After running your code I did fine I had counting errors in cells U14, V14 and T24
</SPAN></SPAN>

Thank you for your kind help
</SPAN></SPAN>

Have a nice weekend
</SPAN></SPAN>

Good Luck
</SPAN></SPAN>

Kind Regards
</SPAN></SPAN>
Kishan :)
</SPAN></SPAN>
 
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,201
Members
453,022
Latest member
RobertV1609

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top