Search "x time" repeated and colour the cells

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>

I want a Formula or may be VBA that can search "x time" for selected value it can be "1", "X", or 2
</SPAN></SPAN>

In the column C, there are data with repeated values in the cells C6 and below
</SPAN></SPAN>

In the cell B1 is entered value "1" (it could be X or 2 also) which to be search in the column C
</SPAN></SPAN>

In the cell B2 is entered value 3 it mean find 3 repeated values "1's" and (it could be 2 to 8) colour the cells
</SPAN></SPAN>

Search method should be striate, for example once 3-repeated value find, look for next 3 do not look back you will see C16, 17, 18 find 3 repeated, but C19, C20 stand alone forget it.
</SPAN></SPAN>

Checked value will be considered B1, and the repeated value of the cell B2
</SPAN></SPAN>

Some example of the data results
</SPAN></SPAN>


Book1
ABCDE
1Check--->1
2Repeated->3
3
4
5P1
6X
71
81
91
10X
11X
122
13X
141
15X
161
171
181
191
201
21X
221
231
241
251
261
27X
281
291
301
311
321
331
341
351
36X
372
381
39X
401
411
421
43X
442
45X
461
47X
481
49X
501
511
521
532
541
551
56X
571
581
591
601
611
622
631
641
651
661
671
681
691
70X
711
721
731
74X
751
762
77X
781
791
801
811
821
831
841
851
861
87X
881
891
90X
911
921
931
941
951
96X
97X
98
99
100
Sheet1


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

Regards,
</SPAN></SPAN>
Kishan
</SPAN></SPAN>
 
Last edited:
How about
Code:
Sub Hilite()
   Dim i As Long, clr As Long, Qty As Long
   Dim Chk As Variant
   Dim Rpt As Long
   
   Chk = Range("B1").Value
   Rpt = Range("B2").Value
   clr = 1
   For i = 6 To Range("C" & Rows.count).End(xlUp).Row
      If Range("C" & i).Value = Chk And Application.CountIf(Range("C" & i).Resize(Rpt), Chk) = Rpt Then
         Range("C" & i).Resize(Rpt).Interior.Color = Choose(clr, vbGreen, vbRed)
         i = i + Rpt - 1: [COLOR=#ff0000][B]clr = IIf(clr = 1, 2, 1)[/B][/COLOR]: Qty = Qty + 1
      End If
   Next i
  Range("C2").Value = Qty
End Sub
The same comment I made in Message #9 would apply here as well. What I highlighted in red can be replaced with the following...

clr = 3 - clr
 
Last edited:
Upvote 0

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
The same comment I made in Message #9 would apply here as well. What I highlighted in red can be replaced with the following...

clr = 3 - clr
Hi Rick, I replaced the line below as you suggested </SPAN></SPAN>

This
</SPAN></SPAN>
Code:
 i = i + Rpt - 1: clr = IIf(clr = 1, 2, 1): Qty = Qty + 1
</SPAN></SPAN>

To this one below
</SPAN></SPAN>
Code:
 i = i + Rpt - 1: clr = 3 - clr: Qty = Qty + 1
</SPAN></SPAN>

And the, Fluff code is resulting same
</SPAN></SPAN>

Thank you for the improved suggestion
</SPAN></SPAN>

Kind Regards,
</SPAN></SPAN>
Kishan :)
</SPAN></SPAN>
 
Upvote 0
The IIf function is a somewhat slow function (in the compiled version of VB, it is literally 5 times slower than doing a standard If..Else block), so if you can avoid it, that would be best.
Thanks for that Rick.
Looks like I'll be going back to the standard If/else :)
 
Upvote 0

Forum statistics

Threads
1,215,372
Messages
6,124,537
Members
449,169
Latest member
mm424

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