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:

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
Hello,

Still working on this right now. But so far i got it to work for 1 and 3 just like the example, maybe you can work off of this...I will continue to work on it no worries lol

Code:
Sub CheckRep()
Dim r As Range
Dim LastRow, ci As Integer
Dim s As Worksheet
Dim c As Boolean
Set s = ActiveSheet
c = False
LastRow = s.Cells(s.Rows.Count, "C").End(xlUp).Row


For i = 6 To LastRow
    If (Cells(i, 3).Value = Cells(i, 3).Offset(1, 0).Value And Cells(i, 3).Offset(1, 0).Value = Cells(i, 3).Offset(2, 0).Value) Then
    If c = False Then ci = 37 Else ci = 40
        Cells(i, 3).Interior.ColorIndex = ci
        Cells(i + 1, 3).Interior.ColorIndex = ci
        Cells(i + 2, 3).Interior.ColorIndex = ci
        i = i + 2
    If c = False Then c = True Else c = False
    End If
Next i


End Sub
 
Upvote 0
How about
Code:
Sub Hilite()
   Dim i As Long, clr 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: clr = IIf(clr = 1, 2, 1)
      End If
   Next i
End Sub
 
Upvote 0
It allows you to put 2 lines of code on 1 line.
So it would normally be written like
Code:
i = i + Rpt - 1
clr = IIf(clr = 1, 2, 1)
 
Upvote 0
How about
Code:
Sub Hilite()
   Dim i As Long, clr 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: clr = IIf(clr = 1, 2, 1)
      End If
   Next i
End Sub
Amazing Fluff, code highlights even only 1 repeated, checked with over 50 repeated no problem!! At all, also it highlight in 2 alternative colours Perfect!!</SPAN></SPAN>

Thank you for your time and help
</SPAN></SPAN>

Have a nice day
</SPAN></SPAN>

Kind Regards,
</SPAN></SPAN>
Kishan :)
</SPAN></SPAN>


Please may I request could you add in it some more information, which I did not thought before
</SPAN></SPAN>

Can be counted highlighted sets, like in the example post#1 there are 15 sets of 3,
</SPAN></SPAN>So the counts 15 can be placed in the cell C2 if possible</SPAN></SPAN>

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

Regards,
</SPAN></SPAN>
Kishan
</SPAN></SPAN>
 
Upvote 0
Hello,
Still working on this right now. But so far i got it to work for 1 and 3 just like the example, maybe you can work off of this...I will continue to work on it no worries lol
Hi Nine Zero, I do appreciate your interest helping to resolve it.</SPAN></SPAN>

Thank you so much
</SPAN></SPAN>

Kind Regards,
</SPAN></SPAN>
Kishan :)
</SPAN></SPAN>
 
Upvote 0
How about
Code:
Sub Hilite()
   Dim i As Long, clr As Long, [COLOR=#ff0000]Qty As Long[/COLOR]
   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: clr = IIf(clr = 1, 2, 1)[COLOR=#ff0000]: Qty = Qty + 1[/COLOR]
      End If
   Next i
   [COLOR=#ff0000]Range("C2").Value = Qty[/COLOR]
End Sub
 
Upvote 0
It allows you to put 2 lines of code on 1 line.
So it would normally be written like
Code:
i = i + Rpt - 1
[B][COLOR="#FF0000"]clr = IIf(clr = 1, 2, 1)[/COLOR][/B]
You can replace the line of code I highlighted in red above with this and your macro will work the same...

clr = 3 - clr

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.
 
Upvote 0
How about
Code:
Sub Hilite()
   Dim i As Long, clr As Long, [COLOR=#ff0000]Qty As Long[/COLOR]
   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: clr = IIf(clr = 1, 2, 1)[COLOR=#ff0000]: Qty = Qty + 1[/COLOR]
      End If
   Next i
   [COLOR=#ff0000]Range("C2").Value = Qty[/COLOR]
End Sub
Hi Fluff, it is a perfect solution.</SPAN></SPAN>

Thank you for assisting twice
</SPAN></SPAN>

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

Forum statistics

Threads
1,223,893
Messages
6,175,240
Members
452,621
Latest member
Laura_PinksBTHFT

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