Results 1 to 8 of 8

Thread: VBA - Condition formating based on condition
Thanks Thanks: 0 Likes Likes: 0

  1. #1
    New Member
    Join Date
    Aug 2016
    Posts
    27
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default VBA - Condition formating based on condition

    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 by kipulbiu; Aug 22nd, 2019 at 04:03 PM.

  2. #2
    Board Regular
    Join Date
    Mar 2016
    Posts
    220
    Post Thanks / Like
    Mentioned
    2 Post(s)
    Tagged
    2 Thread(s)

    Default Re: VBA - Condition formating based on condition

    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

  3. #3
    New Member
    Join Date
    Aug 2016
    Posts
    27
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: VBA - Condition formating based on condition

    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)

  4. #4
    Board Regular
    Join Date
    Mar 2016
    Posts
    220
    Post Thanks / Like
    Mentioned
    2 Post(s)
    Tagged
    2 Thread(s)

    Default Re: VBA - Condition formating based on condition

    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

  5. #5
    New Member
    Join Date
    Aug 2016
    Posts
    27
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: VBA - Condition formating based on condition

    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

  6. #6
    Board Regular
    Join Date
    Mar 2016
    Posts
    220
    Post Thanks / Like
    Mentioned
    2 Post(s)
    Tagged
    2 Thread(s)

    Default Re: VBA - Condition formating based on condition

    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

  7. #7
    New Member
    Join Date
    Aug 2016
    Posts
    27
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: VBA - Condition formating based on condition

    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

  8. #8
    Board Regular
    Join Date
    Mar 2016
    Posts
    220
    Post Thanks / Like
    Mentioned
    2 Post(s)
    Tagged
    2 Thread(s)

    Default Re: VBA - Condition formating based on condition

    You are very welcome. Glad I could help.

Some videos you may like

User Tag List

Tags for this Thread

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •