VBA Automatic Conditional Formatting based on Formula outcome/criteria

ckdragon

New Member
Joined
Apr 3, 2022
Messages
37
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
Hi There.

I am trying to write some VBA code to conditionally format some cells/rows and that will apply across my whole workbook except 2 sheets (sheet 1 & 2) and that will automatically update when things are changed.

They are the results of formulas in the cells.

I know I can use normal conditional formatting but I am working with power query tables and every time the data changes, sometimes it doesn't pick up the formatting.

I have started writing some code below but keep getting "Type Mismatch" errors and cant make it automatically run

Can anyone please have a look and see what I am missing here?

Thank you!

Private Sub FOrmatting()

Dim MyRange As Range
Set MyRange = ActiveSheet.Range("A1:V75")

For Each cell In MyRange

If cell.Value = "Early" Then
cell.Interior.Color = RGB(255, 0, 0)
End If

If cell.Value = "Late" Then
cell.Interior.Color = RGB(255, 0, 0)
End If

If cell.Value = "ERROR" Then
cell.Interior.Color = RGB(255, 0, 0)
End If

If cell.Value = "YES - MANUAL PROCESS" Then
cell.Interior.Color = RGB(255, 0, 0)
End If

If cell.Value = "In Booking Time" Then
cell.Interior.Color = RGB(146, 208, 80)
End If

If cell.Value = "TRUE" Then
cell.Interior.Color = RGB(146, 208, 80)
End If

If cell.Value = "NO - AUTO CALCULATION" Then
cell.Interior.Color = RGB(146, 208, 80)
End If

If cell.Value = "NO" Then
cell.Interior.Color = RGB(146, 208, 80)
End If

If cell.Value = "Acceptably Early" Then
cell.Interior.Color = RGB(255, 217, 102)
End If

If cell.Value = "Acceptably Late" Then
cell.Interior.Color = RGB(255, 217, 102)
End If

If cell.Value = "YES" Then
cell.Interior.Color = RGB(255, 217, 102)
End If

If cell.Value = "Journey started and ended in a Business zone" Then
Rows(cell.Row).Interior.Color = RGB(242, 242, 242)
End If

If cell.Value = "**Jane Doe**" Then
Rows(cell.Row).Interior.Color = RGB(242, 242, 242)
End If

If Cell(cell.row "L").Value = "Base (Business)" Then
cell(cell.D).Interior.Color = RGB(255, 204, 204)
cell(cell.F).Interior.Color = RGB(255, 204, 204)
End If

If Cell(cell. row "K").Value = "Base (Business)" Then
cell(cell.D).Interior.Color = RGB(219, 246, 214)
cell(cell.F).Interior.Color = RGB(219, 246, 214)
End If

Next cell

End Sub
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Hi,

all I could see was that you'd missed a couple of commas in the lower part of your code, and needed few corrections in your last interior color selections.

VBA Code:
Private Sub FOrmatting()

Dim MyRange As Range
Set MyRange = ActiveSheet.Range("A1:V75")

For Each cell In MyRange

If cell.Value = "Early" Then
cell.Interior.Color = RGB(255, 0, 0)
End If

If cell.Value = "Late" Then
cell.Interior.Color = RGB(255, 0, 0)
End If

If cell.Value = "ERROR" Then
cell.Interior.Color = RGB(255, 0, 0)
End If

If cell.Value = "YES - MANUAL PROCESS" Then
cell.Interior.Color = RGB(255, 0, 0)
End If

If cell.Value = "In Booking Time" Then
cell.Interior.Color = RGB(146, 208, 80)
End If

If cell.Value = "TRUE" Then
cell.Interior.Color = RGB(146, 208, 80)
End If

If cell.Value = "NO - AUTO CALCULATION" Then
cell.Interior.Color = RGB(146, 208, 80)
End If

If cell.Value = "NO" Then
cell.Interior.Color = RGB(146, 208, 80)
End If

If cell.Value = "Acceptably Early" Then
cell.Interior.Color = RGB(255, 217, 102)
End If

If cell.Value = "Acceptably Late" Then
cell.Interior.Color = RGB(255, 217, 102)
End If

If cell.Value = "YES" Then
cell.Interior.Color = RGB(255, 217, 102)
End If

If cell.Value = "Journey started and ended in a Business zone" Then
Rows(cell.Row).Interior.Color = RGB(242, 242, 242)
End If

If cell.Value = "**Jane Doe**" Then
Rows(cell.Row).Interior.Color = RGB(242, 242, 242)
End If

If cell(cell.Row, "L").Value = "Base (Business)" Then
cell(cell.Row, "D").Interior.Color = RGB(255, 204, 204)
cell(cell.Row, "F").Interior.Color = RGB(255, 204, 204)
End If

If cell(cell.Row, "K").Value = "Base (Business)" Then
cell(cell.Row, "D").Interior.Color = RGB(219, 246, 214)
cell(cell.Row, "F").Interior.Color = RGB(219, 246, 214)
End If

Next cell

End Sub
 
Upvote 0
Hi,

all I could see was that you'd missed a couple of commas in the lower part of your code, and needed few corrections in your last interior color selections.

VBA Code:
Private Sub FOrmatting()

Dim MyRange As Range
Set MyRange = ActiveSheet.Range("A1:V75")

For Each cell In MyRange

If cell.Value = "Early" Then
cell.Interior.Color = RGB(255, 0, 0)
End If

If cell.Value = "Late" Then
cell.Interior.Color = RGB(255, 0, 0)
End If

If cell.Value = "ERROR" Then
cell.Interior.Color = RGB(255, 0, 0)
End If

If cell.Value = "YES - MANUAL PROCESS" Then
cell.Interior.Color = RGB(255, 0, 0)
End If

If cell.Value = "In Booking Time" Then
cell.Interior.Color = RGB(146, 208, 80)
End If

If cell.Value = "TRUE" Then
cell.Interior.Color = RGB(146, 208, 80)
End If

If cell.Value = "NO - AUTO CALCULATION" Then
cell.Interior.Color = RGB(146, 208, 80)
End If

If cell.Value = "NO" Then
cell.Interior.Color = RGB(146, 208, 80)
End If

If cell.Value = "Acceptably Early" Then
cell.Interior.Color = RGB(255, 217, 102)
End If

If cell.Value = "Acceptably Late" Then
cell.Interior.Color = RGB(255, 217, 102)
End If

If cell.Value = "YES" Then
cell.Interior.Color = RGB(255, 217, 102)
End If

If cell.Value = "Journey started and ended in a Business zone" Then
Rows(cell.Row).Interior.Color = RGB(242, 242, 242)
End If

If cell.Value = "**Jane Doe**" Then
Rows(cell.Row).Interior.Color = RGB(242, 242, 242)
End If

If cell(cell.Row, "L").Value = "Base (Business)" Then
cell(cell.Row, "D").Interior.Color = RGB(255, 204, 204)
cell(cell.Row, "F").Interior.Color = RGB(255, 204, 204)
End If

If cell(cell.Row, "K").Value = "Base (Business)" Then
cell(cell.Row, "D").Interior.Color = RGB(219, 246, 214)
cell(cell.Row, "F").Interior.Color = RGB(219, 246, 214)
End If

Next cell

End Sub
Thank you so much for this. Fixed a few things, but now I am getting a Type Mismatch error (13) -

Identifier under cursor isn't a procedure name - but only on some of my sheets in my workbook.


For note, I went and put the code in a button and there is one on every sheet so it only runs the code when you click the button and its only of the active sheet.

But all the sheets are exactly the same (minus some data) so I dont understand why it is throwing this error on some and not all the sheets

Any help would be great

Thank you!
 
Upvote 0
Hi

I think you will need to share some data, as the code worked fine for me after a couple of small typo fixes ?
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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