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.
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
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
 
Upvote 0
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?
 
Upvote 0
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


Book1
A
1West Yorkshire
2Derbyshire
3Staffordshire
Sheet2



Book1
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
 
Upvote 0
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.
 
Upvote 0
I figured it out! My boss had it set as a conditional format. I undid that and it worked perfectly!
 
Upvote 0
Glad you sorted it out & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,176
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top