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:

Some videos you may like

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.

shknbk2

Board Regular
Joined
Mar 5, 2016
Messages
240
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
 

kipulbiu

New Member
Joined
Aug 14, 2016
Messages
36
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)
 

shknbk2

Board Regular
Joined
Mar 5, 2016
Messages
240
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
 

kipulbiu

New Member
Joined
Aug 14, 2016
Messages
36
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
 

shknbk2

Board Regular
Joined
Mar 5, 2016
Messages
240
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
 

kipulbiu

New Member
Joined
Aug 14, 2016
Messages
36
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
 

kipulbiu

New Member
Joined
Aug 14, 2016
Messages
36
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
 

shknbk2

Board Regular
Joined
Mar 5, 2016
Messages
240
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.
 
Status
Not open for further replies.

Watch MrExcel Video

Forum statistics

Threads
1,101,991
Messages
5,484,064
Members
407,426
Latest member
Owen Chia

This Week's Hot Topics

  • Finding issue in If elseif else with For each Loop
    Finding issue in If elseif else with For each Loop I have tried this below code but i'm getting in Y column filled with W005. Colud you please...
  • MsgBox Error
    Hi Guys, I have the below error show up when i try and run my macro in File1 but works fine if i copy and paste the same code into file2. [ATTACH...
  • CELL FORMAT - IF CONDITION
    My Cell Format is [B]""0.00" Cr". [/B]But in the cell, it is showing 123.00 for editing. (123 is entry figure). (Data imported from other...
  • Show numbers nearly the same
    Is this possible. I have a number that can change very time eg 0.00001234 Then I have a lot of numbers 0.0000001, 0.0000002, 0.00000004...
  • Please i need your help to create formula
    I need a formula in cell B8 to do this >>if b1=1 then multiply ( cell b8) by 10% ,if b1=2 multiply by 20%,if=3 multiply by 30%. Thank you in...
  • Got error while adding column and filter
    Got error while adding column and filter In column Z has some like "Success" and "Error". I want to add column in AA if the Z cell value is...
Top