Referencing a range from another sheet via VBA for Conditional Formatting?

jmpatrick

Active Member
Joined
Aug 17, 2016
Messages
477
Office Version
  1. 365
Platform
  1. Windows
Good morning! I'm trying to learn how to create Conditional Formatting rules using VBA.

I have this simple code that is working:

VBA Code:
Sub VBAConditionalFormatting()
 
  Dim MyRange As Range
  Set MyRange = Worksheets("Calendar").Range("SubLotColumn")
  
  MyRange.FormatConditions.Delete

  For Each cell In MyRange
  If cell.Value Like "Independence Day" Then
     cell.Interior.ColorIndex = 6

End If
Next
End Sub

In order to make it easier to add and delete holidays I'd like to change the If cell.Value Like "Independence Day" Then part to reference a range from another sheet. Something like this:

If cell.Value Like "Worksheets("Holidays").Range("Holidays")" Then

That way I can easily update the list of Holidays and the Conditional Formatting will never have to be touched.

Make sense?
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
I tried this. No luck:

VBA Code:
Sub VBAConditionalFormatting()
 
  Dim MyRange As Range
  Set MyRange = Worksheets("Calendar").Range("SubLotColumn")
 
  Dim MyData As Range
  Set MyData = Worksheets("Holidays").Range("Holidays")
 
  MyRange.FormatConditions.Delete

  For Each cell In MyRange
  If cell.Value Like MyData Then
     cell.Interior.ColorIndex = 6

End If
Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,221
Messages
6,123,701
Members
449,117
Latest member
Aaagu

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