CF, macro require

Kishan

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

VBA, conditional formatting macro in the columns C, D & E as shown colours for pattern in the C1:E4 and in the columns G, H & I as shown colours for pattern in the G3:I3

Example data


Book1
ABCDEFGHIJK
11|11|XX|X1|1X|12|1
21|2X|21|XX|X2|X
3X|12|X1|2X|22|2
42|12|2
5P1P2P3P1P2P3
6X|11|12|1X|11|12|1
7X|X2|1X|XX|X2|1X|X
8X|11|12|1X|11|12|1
92|11|X2|12|11|X2|1
101|X1|X1|11|X1|X1|1
11X|2X|X2|XX|2X|X2|X
12X|11|21|1X|11|21|1
131|XX|21|X1|XX|21|X
141|11|2X|X1|11|2X|X
151|XX|11|11|XX|11|1
16X|1X|21|2X|1X|21|2
171|1X|12|11|1X|12|1
181|11|11|X1|11|11|X
19X|11|XX|1X|11|XX|1
201|11|12|11|11|12|1
211|11|22|11|11|22|1
222|1X|11|X2|1X|11|X
231|11|11|21|11|11|2
24X|11|21|1X|11|21|1
251|11|1X|11|11|1X|1
26X|11|11|2X|11|11|2
271|2X|XX|X1|2X|XX|X
281|11|21|21|11|21|2
291|11|22|11|11|22|1
301|11|X1|11|11|X1|1
311|11|X1|11|11|X1|1
321|X1|12|11|X1|12|1
332|21|1X|12|21|1X|1
341|X1|21|11|X1|21|1
352|11|12|X2|11|12|X
361|XX|21|11|XX|21|1
37X|11|11|1X|11|11|1
38X|21|1X|XX|21|1X|X
39X|12|12|1X|12|12|1
401|11|XX|11|11|XX|1
412|X1|11|12|X1|11|1
42X|2X|11|1X|2X|11|1
43
44
45
46
Sheet1


Thank you in advance

Regards,
Kishan
 
Last edited:

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Why do you need VBA for this? It could be done with 6 conditional formatting rules.
 
Upvote 0
Why do you need VBA for this? It could be done with 6 conditional formatting rules.
6StringJazzer, I am already using formulas in the example i have shown 2 set one in columns C:E & other in columns G:I, </SPAN></SPAN>
But actually it is one set in C:E columns so I want macro,..... if I run A macro it colours cell as per C1:E4, and if I run B macro it colours cell as per G3:I3</SPAN></SPAN>

Hope it is clarified</SPAN></SPAN>
 
Last edited:
Upvote 0
Modify the color codes to suit your choices! I'm not good with the color codes!

Sub color_cells_1()
Dim lr As Long, cell_co As Range
lr = WorksheetFunction.Max(Cells(Rows.Count, 3).End(xlUp).Row, Cells(Rows.Count, 4).End(xlUp).Row, Cells(Rows.Count, 5).End(xlUp).Row)
Set cell_co = Range("C6:E" & lr)
For Each cell In cell_co
If cell.Value2 = "1|1" Then
cell.Interior.Color = vbRed
ElseIf cell.Value2 = "1|x" Or cell.Value2 = "1|2" Or cell.Value2 = "X|1" Or cell.Value2 = "2|1" Then
cell.Interior.Color = vbGreen
ElseIf cell.Value2 = "X|X" Or cell.Value2 = "X|2" Or cell.Value2 = "2|X" Or cell.Value2 = "2|2" Then
cell.Interior.Color = vbBlue
End If
Next
End Sub
Sub color_cells_2()
Dim lr As Long, cell_co As Range
lr = WorksheetFunction.Max(Cells(Rows.Count, 3).End(xlUp).Row, Cells(Rows.Count, 4).End(xlUp).Row, Cells(Rows.Count, 5).End(xlUp).Row)
Set cell_co = Range("C6:E" & lr)
For Each cell In cell_co
If cell.Value2 = "1|1" Or cell.Value2 = "1|X" Or cell.Value2 = "X|1" Then
cell.Interior.Color = vbRed
ElseIf cell.Value2 = "X|1" Or cell.Value2 = "X|X" Or cell.Value2 = "X|2" Then
cell.Interior.Color = vbGreen
ElseIf cell.Value2 = "2|1" Or cell.Value2 = "2|X" Or cell.Value2 = "2|2" Then
cell.Interior.Color = vbBlue
End If
Next
End Sub
 
Upvote 0
I do not know if the following will work in your version of Excel (XL2000) or not, but it works fine in my version of Excel (XL2010). Try it and let me know...
Code:
[table="width: 500"]
[tr]
	[td]Sub ColorCells()
  Dim Cell As Range, Rng(-1 To 0) As Range
  Set Rng(-1) = Range("C10", Cells(Rows.Count, "E").End(xlUp))
  Set Rng(0) = Range("G10", Cells(Rows.Count, "I").End(xlUp))
  Application.ReplaceFormat.Clear
  For Each Cell In Range("C5:I8")
    If Cell.Interior.ColorIndex <> xlColorIndexNone Then
      Application.ReplaceFormat.Interior.Color = Cell.Interior.Color
      Application.ReplaceFormat.Font.Color = vbWhite
      Application.ReplaceFormat.Font.Bold = True
      Rng(Cell.Column < 6).Replace Cell.Value, "", SearchFormat:=False, ReplaceFormat:=True
    End If
  Next
  Application.ReplaceFormat.Clear
End Sub[/td]
[/tr]
[/table]
 
Upvote 0
I do not know if the following will work in your version of Excel (XL2000) or not, but it works fine in my version of Excel (XL2010). Try it and let me know...
Hi Rick, code stop at "SearchFormat:=False," if I eliminate this than stop at "ReplaceFormat:=True" and popup message Argument do not find
 
Upvote 0
Modify the color codes to suit your choices! I'm not good with the color codes!
Hi KolGuyXcel, thank you it worked just modified the colours as I needed</SPAN></SPAN>
Code:
Sub color_cells_1()
Dim lr As Long, cell_co As Range
lr = WorksheetFunction.Max(Cells(Rows.Count, 3).End(xlUp).Row, Cells(Rows.Count, 4).End(xlUp).Row, Cells(Rows.Count, 5).End(xlUp).Row)
Set cell_co = Range("C6:E" & lr)
For Each Cell In cell_co
If Cell.Value2 = "1|1" Then
'Cell.Interior.Color = vbRed
[COLOR=#ff0000]Cell.Interior.ColorIndex = 3
Cell.Font.ColorIndex = 2
[/COLOR]ElseIf Cell.Value2 = "1|X" Or Cell.Value2 = "1|2" Or Cell.Value2 = "X|1" Or Cell.Value2 = "2|1" Then
'Cell.Interior.Color = vbGreen
[COLOR=#006400]Cell.Interior.ColorIndex = 10
Cell.Font.ColorIndex = 2
[/COLOR]ElseIf Cell.Value2 = "X|X" Or Cell.Value2 = "X|2" Or Cell.Value2 = "2|X" Or Cell.Value2 = "2|2" Then
'Cell.Interior.Color = vbBlue
[COLOR=#0000ff]Cell.Interior.ColorIndex = 5
Cell.Font.ColorIndex = 2
[/COLOR]End If
Next
End Sub


Sub color_cells_2()
Dim lr As Long, cell_co As Range
lr = WorksheetFunction.Max(Cells(Rows.Count, 3).End(xlUp).Row, Cells(Rows.Count, 4).End(xlUp).Row, Cells(Rows.Count, 5).End(xlUp).Row)
Set cell_co = Range("C6:E" & lr)
For Each Cell In cell_co
If Cell.Value2 = "1|1" Or Cell.Value2 = "1|X" Or Cell.Value2 = "1|2" Then
'Cell.Interior.Color = vbRed
[COLOR=#ff0000]Cell.Interior.ColorIndex = 3
Cell.Font.ColorIndex = 2
[/COLOR]ElseIf Cell.Value2 = "X|1" Or Cell.Value2 = "X|X" Or Cell.Value2 = "X|2" Then
'Cell.Interior.Color = vbGreen
[COLOR=#006400]Cell.Interior.ColorIndex = 10
Cell.Font.ColorIndex = 2
[/COLOR]ElseIf Cell.Value2 = "2|1" Or Cell.Value2 = "2|X" Or Cell.Value2 = "2|2" Then
'Cell.Interior.Color = vbBlue
[COLOR=#0000ff]Cell.Interior.ColorIndex = 5
Cell.Font.ColorIndex = 2[/COLOR]
End If
Next
End Sub


Regards,</SPAN></SPAN>
Kishan </SPAN></SPAN>
 
Upvote 0
Hi Rick, code stop at "SearchFormat:=False," if I eliminate this than stop at "ReplaceFormat:=True" and popup message Argument do not find
Yeah, that is what I was afraid of. How about this code... does it work on your system?
Code:
[table="width: 500"]
[tr]
	[td]Sub ColorCells()
  Dim X As Long, LastRow As Long, Cell As Range, Codes As String, Dict(-1 To 0) As Object
  Set Dict(-1) = CreateObject("Scripting.Dictionary")
  Set Dict(0) = CreateObject("Scripting.Dictionary")
  For Each Cell In Range("C5:I8")
    If Len(Cell.Value) Then Dict(Cell.Column < 6).Item(Cell.Value) = Cell.Interior.Color
  Next
  LastRow = Cells.Find("*", , xlValues, , xlRows, xlPrevious, , , False).Row
  Application.ScreenUpdating = False
  For Each Cell In Union(Range("C10:E" & LastRow), Range("G10:I" & LastRow))
    If Len(Cell.Value) Then
      Cell.Interior.Color = Dict(Cell.Column < 6).Item(Cell.Value)
      Cell.Font.Color = vbWhite
      Cell.Font.Bold = True
    End If
  Next
  Application.ScreenUpdating = True
End Sub[/td]
[/tr]
[/table]
 
Upvote 0
Yeah, that is what I was afraid of. How about this code... does it work on your system?
Hi Rick, thank you code stuck at ".Find" pop up message number of arguments are not correct if I do modify "xlPrevious, , , False" to "xlPrevious, False" code run and present result as shown below


Book1
ABCDEFGHIJK
11|11|XX|X1|1X|12|1
21|2X|21|XX|X2|X
3X|12|X1|2X|22|2
42|12|2
5P1P2P3P1P2P3
6X|11|12|1X|11|12|1
7X|X2|1X|XX|X2|1X|X
8X|11|12|1X|11|12|1
92|11|X2|12|11|X2|1
101|X1|X1|11|X1|X1|1
11X|2X|X2|XX|2X|X2|X
12X|11|21|1X|11|21|1
131|XX|21|X1|XX|21|X
141|11|2X|X1|11|2X|X
151|XX|11|11|XX|11|1
16X|1X|21|2X|1X|21|2
171|1X|12|11|1X|12|1
181|11|11|X1|11|11|X
19X|11|XX|1X|11|XX|1
201|11|12|11|11|12|1
211|11|22|11|11|22|1
222|1X|11|X2|1X|11|X
231|11|11|21|11|11|2
24X|11|21|1X|11|21|1
251|11|1X|11|11|1X|1
26X|11|11|2X|11|11|2
271|2X|XX|X1|2X|XX|X
281|11|21|21|11|21|2
291|11|22|11|11|22|1
301|11|X1|11|11|X1|1
311|11|X1|11|11|X1|1
321|X1|12|11|X1|12|1
332|21|1X|12|21|1X|1
341|X1|21|11|X1|21|1
352|11|12|X2|11|12|X
361|XX|21|11|XX|21|1
37X|11|11|1X|11|11|1
38X|21|1X|XX|21|1X|X
39X|12|12|1X|12|12|1
401|11|XX|11|11|XX|1
412|X1|11|12|X1|11|1
42X|2X|11|1X|2X|11|1
43
44
45
46
Sheet1


Regards,
Kishan
 
Upvote 0
Hi Rick, thank you code stuck at ".Find" pop up message number of arguments are not correct
Okay, how about this then....
Code:
[table="width: 500"]
[tr]
	[td]Sub ColorCells()
  Dim X As Long, LastRow As Long, Cell As Range, Codes As String, Dict(-1 To 0) As Object
  Set Dict(-1) = CreateObject("Scripting.Dictionary")
  Set Dict(0) = CreateObject("Scripting.Dictionary")
  For Each Cell In Range("C5:I8")
    If Len(Cell.Value) Then Dict(Cell.Column < 6).Item(Cell.Value) = Cell.Interior.Color
  Next
  LastRow = Cells.Find("*", , xlValues, , xlRows, xlPrevious, , , False).Row
  Application.ScreenUpdating = False
  For Each Cell In Union(Range("C10:E" & LastRow), Range("G10:I" & LastRow))
    If Len(Cell.Value) Then
      Cell.Interior.Color = Dict(Cell.Column < 6).Item(Cell.Value)
      Cell.Font.Color = vbWhite
      Cell.Font.Bold = True
    End If
  Next
  Application.ScreenUpdating = True
End Sub[/td]
[/tr]
[/table]
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,853
Members
452,361
Latest member
d3ad3y3

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