in each row count same alphabet in cells , as soon as it counts 6 next cell marked as WO

kshitij_dch

Active Member
Joined
Apr 1, 2012
Messages
362
Office Version
  1. 365
  2. 2016
  3. 2007
Platform
  1. Windows
Hello All,
I am working on one logic , need help
i have a range Range A:AE
Have Certain codes (alphabets) on cells like "P" , "SL" , "TL" , "MK"
i want to apply logic that starting from cell A1 or A2 or A3......


macro should check for code "P" in cells , as soon as it finds 6 "P" in a row (No Gap), next cell should automatically marked as "WO".


Please suggest ....here is what i want

[TABLE="width: 832"]
<colgroup><col width="64" span="13" style="width:48pt"> </colgroup><tbody>[TR]
[TD="width: 64"]P[/TD]
[TD="width: 64"]p[/TD]
[TD="width: 64"]p[/TD]
[TD="width: 64"]p[/TD]
[TD="width: 64"]p[/TD]
[TD="width: 64"]p[/TD]
[TD="width: 64"]WO[/TD]
[TD="width: 64"][/TD]
[TD="width: 64"][/TD]
[TD="width: 64"][/TD]
[TD="width: 64"][/TD]
[TD="width: 64"][/TD]
[TD="width: 64"][/TD]
[/TR]
[TR]
[TD]p[/TD]
[TD][/TD]
[TD]p[/TD]
[TD][/TD]
[TD]p[/TD]
[TD][/TD]
[TD]p[/TD]
[TD]p[/TD]
[TD]p[/TD]
[TD]p[/TD]
[TD]p[/TD]
[TD]p[/TD]
[TD]WO[/TD]
[/TR]
</tbody>[/TABLE]
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Can't think of formula but this VBA would do it (test on copy of data)

Code:
Sub SetWO()
Dim lr As Long 'last row
Dim r As Long, c As Long ' row and column counters
Dim cn As Integer 'counter for 'p'
    
    'get last used row in column A
    lr = Range("A" & Rows.Count).End(xlUp).Row
    
    'loop through all rows
    For r = 1 To lr
        'loop through all columns from A to AE
        For c = 1 To 31
            'if current cell is 'P' then add to counter
            If UCase(Cells(r, c)) = "P" Then
                cn = cn + 1
                'if we have 6 ps in a row next cell is WO
                If cn = 6 Then
                    Cells(r, c + 1) = "WO" 'QUESTION: What if there is a 7th 'P'?
                    cn = 0
                End If
            Else
                cn = 0
            End If
        Next c
    Next r
End Sub
 
Upvote 0
This is working perfectly !! Thanks Gallen

If their is a 7th P in a row ideally macro should replace 7th P with WO ? it it possible ?
 
Upvote 0
Just noticed a potential bug, dependant on how you want it to work. If for example the last 3 columns of row 1 are 'P' and the first 3 columns of row 2 are 'P' then then fourth column will be "WO"

If you need to reset the counter for each row just put cn = 0 beneath the 'For r = 1 to lr' line
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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