VBA - Condition formating based on condition

Status
Not open for further replies.

kipulbiu

New Member
Joined
Aug 14, 2016
Messages
36
Good evening,

I have a column C3 in which I enter a value and a range (BU:CI) in which I also enter a value for each column.
For each row (10000) I would like a macro to copy the formtting ( interior and font ) of C3 value if this value is the same in the Range (BU:CI)

I have tried to write the following code. It works for the first one but when I add to it does not work any longer.

Could you please help?

Thanks in advance

Bruno


Private Sub CommandButton1_Click()
Dim i As Integer


For i = 3 To 10000


If Cells(i, 3).Value = Cells(i, 73).Value Then
Cells(i, 73).Interior.ColorIndex = Cells(i, 3).Interior.ColorIndex
Cells(i, 73).Font.Color = Cells(i, 3).Font.Color


If Cells(i, 3).Value = Cells(i, 74).Value Then
Cells(i, 74).Interior.ColorIndex = Cells(i, 3).Interior.ColorIndex
Cells(i, 74).Font.Color = Cells(i, 3).Font.Color


If Cells(i, 3).Value = Cells(i, 75).Value Then
Cells(i, 75).Interior.ColorIndex = Cells(i, 3).Interior.ColorIndex
Cells(i, 75).Font.Color = Cells(i, 3).Font.Color


If Cells(i, 3).Value = Cells(i, 76).Value Then
Cells(i, 76).Interior.ColorIndex = Cells(i, 3).Interior.ColorIndex
Cells(i, 76).Font.Color = Cells(i, 3).Font.Color


If Cells(i, 3).Value = Cells(i, 77).Value Then
Cells(i, 77).Interior.ColorIndex = Cells(i, 3).Interior.ColorIndex
Cells(i, 77).Font.Color = Cells(i, 3).Font.Color


If Cells(i, 3).Value = Cells(i, 78).Value Then
Cells(i, 78).Interior.ColorIndex = Cells(i, 3).Interior.ColorIndex
Cells(i, 78).Font.Color = Cells(i, 3).Font.Color




If Cells(i, 3).Value = Cells(i, 79).Value Then
Cells(i, 79).Interior.ColorIndex = Cells(i, 3).Interior.ColorIndex
Cells(i, 79).Font.Color = Cells(i, 3).Font.Color


If Cells(i, 3).Value = Cells(i, 80).Value Then
Cells(i, 80).Interior.ColorIndex = Cells(i, 3).Interior.ColorIndex
Cells(i, 80).Font.Color = Cells(i, 3).Font.Color


If Cells(i, 3).Value = Cells(i, 81).Value Then
Cells(i, 81).Interior.ColorIndex = Cells(i, 3).Interior.ColorIndex
Cells(i, 81).Font.Color = Cells(i, 3).Font.Color


If Cells(i, 3).Value = Cells(i, 82).Value Then
Cells(i, 82).Interior.ColorIndex = Cells(i, 3).Interior.ColorIndex
Cells(i, 82).Font.Color = Cells(i, 3).Font.Color


If Cells(i, 3).Value = Cells(i, 82).Value Then
Cells(i, 83).Interior.ColorIndex = Cells(i, 3).Interior.ColorIndex
Cells(i, 83).Font.Color = Cells(i, 3).Font.Color


If Cells(i, 3).Value = Cells(i, 82).Value Then
Cells(i, 84).Interior.ColorIndex = Cells(i, 3).Interior.ColorIndex
Cells(i, 84).Font.Color = Cells(i, 3).Font.Color


If Cells(i, 3).Value = Cells(i, 85).Value Then
Cells(i, 85).Interior.ColorIndex = Cells(i, 3).Interior.ColorIndex
Cells(i, 85).Font.Color = Cells(i, 3).Font.Color


If Cells(i, 3).Value = Cells(i, 86).Value Then
Cells(i, 86).Interior.ColorIndex = Cells(i, 3).Interior.ColorIndex
Cells(i, 86).Font.Color = Cells(i, 3).Font.Color


If Cells(i, 3).Value = Cells(i, 87).Value Then
Cells(i, 87).Interior.ColorIndex = Cells(i, 3).Interior.ColorIndex
Cells(i, 87).Font.Color = Cells(i, 3).Font.Color


End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If






Next i




End Sub
 
Last edited:

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
This should simplify your code a bit. Does it work how you want it?

Code:
Private Sub CommandButton1_Click()
    Dim iRow As Integer, iCol As Integer
    
    For iRow = 3 To 10000
        For iCol = 73 To 87
            If Cells(iRow, 3).Value = Cells(iRow, iCol).Value Then
                Cells(iRow, iCol).Interior.ColorIndex = Cells(iRow, 3).Interior.ColorIndex
                Cells(iRow, iCol).Font.Color = Cells(iRow, 3).Font.Color
            End If
        Next iCol
    Next iRow
End Sub
 
Upvote 0
Hello,

Thank you so much for taking the time to write the code. The code works like a dream. (smooth and very quick). This is really of great help.

Could I please ask you what the code would become if instead of having only one column C3 I had 5 columns ( C3,D3,E3,F3,G3 ). ( see my original post below for reference )

Thanks in advance

Bruno

I have a column C3 in which I enter a value and a range (BU:CI) in which I also enter a value for each column.
For each row (10000) I would like a macro to copy the formtting ( interior and font ) of C3 value if this value is the same in the Range (BU:CI)
 
Upvote 0
I think this is what you need.

Code:
Private Sub CommandButton1_Click()
    Dim iRow As Integer
    Dim iTargetCol As Integer
    Dim iSourceCol As Integer
    
    For iRow = 3 To 10000
        For iSourceCol = 3 To 7 'columns C:G
            For iTargetCol = 73 To 87 'columns BU:CI
                If Cells(iRow, iTargetCol).Value = Cells(iRow, iSourceCol).Value Then
                    Cells(iRow, iTargetCol).Interior.ColorIndex = Cells(iRow, iSourceCol).Interior.ColorIndex
                    Cells(iRow, iTargetCol).Font.Color = Cells(iRow, iSourceCol).Font.Color
                End If
            Next iTargetCol
        Next iSourceCol
    Next iRow
End Sub
 
Upvote 0
Good evening,

Thank you very much indeed. The code works absolutely brilliantly. This is going to save me hours of work. I am extremely grateful.
I have a final question if I may. I have three sheets named: ( saisieso, base3 and partants ) which all contain the same information on column C: G and BU:CI
Is it possible for the code you wrote to also colour the other sheets. How can a similar code carry the same task in three sheets at the same time. I hope my explanation is clear enough.

Thanks again for the time you took to write the code. It is much appreciated.

Bruno
 
Upvote 0
This will set the cells in columns BU:CI in all three sheets if the IF block is true.

Code:
Private Sub CommandButton1_Click()
    Dim iRow As Integer
    Dim iTargetCol As Integer
    Dim iSourceCol As Integer
    
    For iRow = 3 To 10000
        For iSourceCol = 3 To 7 'columns C:G
            For iTargetCol = 73 To 87 'columns BU:CI
                If Cells(iRow, iTargetCol).Value = Cells(iRow, iSourceCol).Value Then
                    Worksheets("saisieso").Cells(iRow, iTargetCol).Interior.ColorIndex = Cells(iRow, iSourceCol).Interior.ColorIndex
                    Worksheets("saisieso").Cells(iRow, iTargetCol).Font.Color = Cells(iRow, iSourceCol).Font.Color
                    Worksheets("base3").Cells(iRow, iTargetCol).Interior.ColorIndex = Cells(iRow, iSourceCol).Interior.ColorIndex
                    Worksheets("base3").Cells(iRow, iTargetCol).Font.Color = Cells(iRow, iSourceCol).Font.Color
                    Worksheets("partants").Cells(iRow, iTargetCol).Interior.ColorIndex = Cells(iRow, iSourceCol).Interior.ColorIndex
                    Worksheets("partants").Cells(iRow, iTargetCol).Font.Color = Cells(iRow, iSourceCol).Font.Color
                End If
            Next iTargetCol
        Next iSourceCol
    Next iRow
End Sub
 
Upvote 0
Good afternnon,
Thanks ever so much. The code is working really well. Thanks for all your precious help.
I have waited such a long time to get a response to my queries and you have helped me so much saving me hours.
I hope this is also uselful to others who are in the same position as me.
Thanks again and best of luck
Bruno
 
Upvote 0
You are very welcome. Glad I could help.

Good Morning,

I was wondering whether you could help. I have written the following code (see below) but it is not working on the line of code highlighted.
I posted a new thread yesterday.

Thanks in advance


Private Sub methodes_Click()
Dim i As Integer
For i = 10 To 1000
' Paris Turf - Si F = y et c egale soit 3,8,11,17 alors i - methode 2
If Cells(i, 6).Value = "Y" And Cells(i, 3).Value = 3 Or Cells(i, 6).Value = "Y" And Cells(i, 3).Value = 8 Or Cells(i, 6).Value = "Y" And Cells(i, 3).Value = 11 Or Cells(i, 6).Value = "Y" And Cells(i, 3).Value = 17 Then
Cells(i, 29).Value = Cells(i, 9).Value
Cells(i, 29).Interior.ColorIndex = Cells(i, 9).Interior.ColorIndex
Cells(i, 29).Font.Color = Cells(i, 9).Font.Color
End If
Next i
 
Upvote 0
Let's not continue in this thread but use the new thread you posted. Please address the code that Fluff posted there to let him know if it works as you want.
 
Upvote 0
Status
Not open for further replies.

Forum statistics

Threads
1,213,482
Messages
6,113,908
Members
448,531
Latest member
mu88forum

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