Conditional Formatting with VBA

ERed1

Board Regular
Joined
Jun 26, 2019
Messages
104
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
Joined
Jun 12, 2014
Messages
30,533
Office Version
365
Platform
Windows
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
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
30,533
Office Version
365
Platform
Windows
It does for me :)

Care to supply some more information, rather than just
That did not work for me.
 

ERed1

Board Regular
Joined
Jun 26, 2019
Messages
104
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
Joined
Jun 12, 2014
Messages
30,533
Office Version
365
Platform
Windows
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
Joined
Jun 26, 2019
Messages
104
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
Joined
Jun 26, 2019
Messages
104
I figured it out! My boss had it set as a conditional format. I undid that and it worked perfectly!
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
30,533
Office Version
365
Platform
Windows
Glad you sorted it out & thanks for the feedback.
 

Forum statistics

Threads
1,078,450
Messages
5,340,357
Members
399,371
Latest member
wilbot

Some videos you may like

This Week's Hot Topics

  • Problem with Radio Button's format control
    I am creating an employee evaluation template (a sample is below) Column A is the category Column B, C D, E and F will be ratings (unacceptable...
  • Last Display on userform to a Listbox
    [CODE=vba] lstdisplay.ColumnCount = 15 lstdisplay.RowSource = "A1:O600000" [/CODE] So when i do this it Displays everything on the sheet i am...
  • Rename and move files to a new location
    Dear all, I have an excel file with the following information. The actual file name is at column A but i want to rename it using the following...
  • Help with True/False Formula
    Hello! Am stumped how to fix this formula, in which my result returns 'True', but it should return False. =IF(AG2=True...
  • Clear extra characters from a provided range of cells
    Dear All, I have following code which gives me desired output to remove extra characters from a provided range. But it takes too much time when...
  • Help with Current and highest streaks
    Hi there, I've just joined the forum and this is my first post. I've already spent quite a bit of time searching the net and this forum for a...
Top