Conditional Formatting with VBA

ERed1

Board Regular
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.
 

Fluff

MrExcel MVP, Moderator
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
 

ERed1

Board Regular
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?
 

Fluff

MrExcel MVP, Moderator
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

<b></b><table cellpadding="2.5px" rules="all" style=";background-color: rgb(255,255,255);border: 1px solid;border-collapse: collapse; border-color: rgb(187,187,187)"><colgroup><col width="25px" style="background-color: rgb(218,231,245)" /><col /></colgroup><thead><tr style=" background-color: rgb(218,231,245);text-align: center;color: rgb(22,17,32)"><th></th><th>A</th></tr></thead><tbody><tr ><td style="color: rgb(22,17,32);text-align: center;">1</td><td style="color: #FF0000;background-color: #FFFF00;;">West Yorkshire</td></tr><tr ><td style="color: rgb(22,17,32);text-align: center;">2</td><td style="font-weight: bold;color: #FFFFFF;background-color: #8DB4E2;;">Derbyshire</td></tr><tr ><td style="color: rgb(22,17,32);text-align: center;">3</td><td style="text-decoration: underline;color: #C5D9F1;background-color: #60497A;;">Staffordshire</td></tr></tbody></table><p style="width:4.8em;font-weight:bold;margin:0;padding:0.2em 0.6em 0.2em 0.5em;border: 1px solid rgb(187,187,187);border-top:none;text-align: center;background-color: rgb(218,231,245);color: rgb(22,17,32)">Sheet2</p><br /><br />

<b></b><table cellpadding="2.5px" rules="all" style=";background-color: rgb(255,255,255);border: 1px solid;border-collapse: collapse; border-color: rgb(187,187,187)"><colgroup><col width="25px" style="background-color: rgb(218,231,245)" /><col /></colgroup><thead><tr style=" background-color: rgb(218,231,245);text-align: center;color: rgb(22,17,32)"><th></th><th>M</th></tr></thead><tbody><tr ><td style="color: rgb(22,17,32);text-align: center;">1</td><td style=";">County</td></tr><tr ><td style="color: rgb(22,17,32);text-align: center;">2</td><td style=";">Cumbria</td></tr><tr ><td style="color: rgb(22,17,32);text-align: center;">3</td><td style="color: #FFFFFF;background-color: #8DB4E2;;">Derbyshire</td></tr><tr ><td style="color: rgb(22,17,32);text-align: center;">4</td><td style="color: #FFFFFF;background-color: #8DB4E2;;">Derbyshire</td></tr><tr ><td style="color: rgb(22,17,32);text-align: center;">5</td><td style="color: #FF0000;background-color: #FFFF00;;">West Yorkshire</td></tr><tr ><td style="color: rgb(22,17,32);text-align: center;">6</td><td style="color: #FFFFFF;background-color: #8DB4E2;;">Derbyshire</td></tr><tr ><td style="color: rgb(22,17,32);text-align: center;">7</td><td style="color: #FFFFFF;background-color: #8DB4E2;;">Derbyshire</td></tr><tr ><td style="color: rgb(22,17,32);text-align: center;">8</td><td style="color: #FF0000;background-color: #FFFF00;;">West Yorkshire</td></tr><tr ><td style="color: rgb(22,17,32);text-align: center;">9</td><td style="color: #FFFFFF;background-color: #8DB4E2;;">Derbyshire</td></tr><tr ><td style="color: rgb(22,17,32);text-align: center;">10</td><td style="color: #C5D9F1;background-color: #60497A;;">Staffordshire</td></tr><tr ><td style="color: rgb(22,17,32);text-align: center;">11</td><td style="color: #FFFFFF;background-color: #8DB4E2;;">Derbyshire</td></tr><tr ><td style="color: rgb(22,17,32);text-align: center;">12</td><td style="color: #FFFFFF;background-color: #8DB4E2;;">Derbyshire</td></tr><tr ><td style="color: rgb(22,17,32);text-align: center;">13</td><td style=";">Cheshire</td></tr><tr ><td style="color: rgb(22,17,32);text-align: center;">14</td><td style="color: #FF0000;background-color: #FFFF00;;">West Yorkshire</td></tr><tr ><td style="color: rgb(22,17,32);text-align: center;">15</td><td style="color: #FF0000;background-color: #FFFF00;;">West Yorkshire</td></tr><tr ><td style="color: rgb(22,17,32);text-align: center;">16</td><td style="color: #FF0000;background-color: #FFFF00;;">West Yorkshire</td></tr><tr ><td style="color: rgb(22,17,32);text-align: center;">17</td><td style="color: #C5D9F1;background-color: #60497A;;">Staffordshire</td></tr><tr ><td style="color: rgb(22,17,32);text-align: center;">18</td><td style=";">Somerset</td></tr><tr ><td style="color: rgb(22,17,32);text-align: center;">19</td><td style=";">Cumbria</td></tr><tr ><td style="color: rgb(22,17,32);text-align: center;">20</td><td style=";">Shropshire</td></tr></tbody></table><p style="width:4.8em;font-weight:bold;margin:0;padding:0.2em 0.6em 0.2em 0.5em;border: 1px solid rgb(187,187,187);border-top:none;text-align: center;background-color: rgb(218,231,245);color: rgb(22,17,32)">Sheet1</p><br /><br />
 

ERed1

Board Regular
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.
 

ERed1

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

Some videos you may like

This Week's Hot Topics

  • Get External Data (long shot question!)
    This is likely a long shot but I am wondering if it is at all possible for Excel to somehow 'change' the contents of a URL that is being linked to...
  • Importing multiple excel files into one spreadsheet
    Hi, I'm trying to import multiple excel files (with the same format into a single spreadsheet) so that each day's file is listed underneath the...
  • Cell Formatting
    Good Morning, I need to format a few different cells in the following manners: A1 has to always add a colon (:) after whatever is typed in by a...
  • How to copy multiple rows using If
    Hi all, I'm very new to VBA and have written this simple code to copy certain cells if a certain cell within that row contains any data. I need...
  • Workbook_Change stopped working !
    I am working on an app to speed up & automate processing of Credit Cards statements. After data is input from a CSV file, it is presented to the...
  • VBA If statement
    Dear All, I have two dates, where I'd like a message box to pop, if the dates are between this criteria. [CODE] sDate1 = #10/1/2019#...
Top