Results 1 to 10 of 10

Thread: Conditional Formatting with VBA

  1. #1
    Board Regular
    Join Date
    Jun 2019
    Posts
    104
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Conditional Formatting with VBA

    Hello all,

    So in column M on sheet 1 I have data that is in no specific order and the amount is always chaning. The data is Dog, Cat, and Fish. On Sheet 2 starting in A1 going to A3, I have Dog, Cat, and Fish in their own columns. They each have a specific cell color and font color. I want there formatting from sheet 2 to go to the cells on sheet 1. Is there a way to do this with a VBA marco?

    Thanks in advance.

  2. #2
    MrExcel MVP
    Moderator
    Fluff's Avatar
    Join Date
    Jun 2014
    Location
    Chippenham
    Posts
    28,353
    Post Thanks / Like
    Mentioned
    471 Post(s)
    Tagged
    47 Thread(s)

    Default Re: Conditional Formatting with VBA

    How about
    Code:
    Sub ERed1()
       Dim Cl As Range
       Dim Dic As Object
       
       Set Dic = CreateObject("scripting.dictionary")
       With Sheets("Sheet2")
          For Each Cl In .Range("A1", .Range("A" & Rows.Count).End(xlUp))
             Dic.Add Cl.Value, Array(Cl.Interior.Color, Cl.Font.Color)
          Next Cl
       End With
       With Sheets("Sheet1")
          For Each Cl In .Range("M2", .Range("M" & Rows.Count).End(xlUp))
             If Dic.exists(Cl.Value) Then
                Cl.Interior.Color = Dic.item(Cl.Value)(0)
                Cl.Font.Color = Dic.item(Cl.Value)(1)
             End If
          Next Cl
       End With
    End Sub
    - Posting Data try one of these tools
    - Posting guidelines, forum rules and terms of use
    - Read the FAQs

    Running Office 365 on Win 10

  3. #3
    Board Regular
    Join Date
    Jun 2019
    Posts
    104
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Conditional Formatting with VBA

    That did not work for me.

  4. #4
    MrExcel MVP
    Moderator
    Fluff's Avatar
    Join Date
    Jun 2014
    Location
    Chippenham
    Posts
    28,353
    Post Thanks / Like
    Mentioned
    471 Post(s)
    Tagged
    47 Thread(s)

    Default Re: Conditional Formatting with VBA

    It does for me

    Care to supply some more information, rather than just
    That did not work for me.
    - Posting Data try one of these tools
    - Posting guidelines, forum rules and terms of use
    - Read the FAQs

    Running Office 365 on Win 10

  5. #5
    Board Regular
    Join Date
    Jun 2019
    Posts
    104
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Conditional Formatting with VBA

    Quote Originally Posted by Fluff View Post
    It does for me

    Care to supply some more information, rather than just
    So when I changed it to my data it did not work. I then made another worksheet and tested everything and it worked fine. I was messing around with it I then copied my data over to the example worksheet so that nothing else could be interfering with it. Do the colors mater for the code? Does the character count matter? Or maybe does the character color mater? The all have to be black?

  6. #6
    MrExcel MVP
    Moderator
    Fluff's Avatar
    Join Date
    Jun 2014
    Location
    Chippenham
    Posts
    28,353
    Post Thanks / Like
    Mentioned
    471 Post(s)
    Tagged
    47 Thread(s)

    Default Re: Conditional Formatting with VBA

    The code takes the font & fill colour from the cells in sheet2 & applies them to the cells in sheet1 where the cell content matches. Like

    A
    1West Yorkshire
    2Derbyshire
    3Staffordshire

    Sheet2





    M
    1County
    2Cumbria
    3Derbyshire
    4Derbyshire
    5West Yorkshire
    6Derbyshire
    7Derbyshire
    8West Yorkshire
    9Derbyshire
    10Staffordshire
    11Derbyshire
    12Derbyshire
    13Cheshire
    14West Yorkshire
    15West Yorkshire
    16West Yorkshire
    17Staffordshire
    18Somerset
    19Cumbria
    20Shropshire

    Sheet1



    - Posting Data try one of these tools
    - Posting guidelines, forum rules and terms of use
    - Read the FAQs

    Running Office 365 on Win 10

  7. #7
    Board Regular
    Join Date
    Jun 2019
    Posts
    104
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Conditional Formatting with VBA

    So on my sheet it isn't allowing the formatting to show. It shows a blank background until the button is pressed. After the button is pressed it pastes a white background instead of the correct color, but if the characters are cleared from the cell (ie fish) then the correct background color shows.

  8. #8
    Board Regular
    Join Date
    Jun 2019
    Posts
    104
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Conditional Formatting with VBA

    I figured it out! My boss had it set as a conditional format. I undid that and it worked perfectly!

  9. #9
    Board Regular
    Join Date
    Jun 2019
    Posts
    104
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Conditional Formatting with VBA

    Thanks for all of the help Fluff!

  10. #10
    MrExcel MVP
    Moderator
    Fluff's Avatar
    Join Date
    Jun 2014
    Location
    Chippenham
    Posts
    28,353
    Post Thanks / Like
    Mentioned
    471 Post(s)
    Tagged
    47 Thread(s)

    Default Re: Conditional Formatting with VBA

    Glad you sorted it out & thanks for the feedback.
    - Posting Data try one of these tools
    - Posting guidelines, forum rules and terms of use
    - Read the FAQs

    Running Office 365 on Win 10

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
  •