VBA Data Validation with Conditional Formatting

CaptainKen

New Member
Joined
Oct 31, 2023
Messages
23
Office Version
  1. 365
Platform
  1. Windows
Hope someone can please help.

1. The Conditional Formatting code below works on this one sheet with Sheet Protection Off. However, with Sheet Protection On, it generates "Run-time error1004: Application defined or object-defined error".

Please see this video for details. Note that I neglected to point out in the video that I get the error on ANY cell in sheet I click on.
Video detailing the issues

2. It would also be appreciated if the code would work with multiple ranges (G5:G9, G11:G13, G17:G26, etc) which are exactly the same across the 11 month (accept "Jan") tabs.

VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)

'Definining the variables:
  Dim Rng01 As Range
  Dim condition As FormatCondition

'Setting the range on which conditional formatting is to be desired
  Set Rng01 = Range("G5:G9")

'To delete/clear any existing conditional formatting from the range
   Rng01.FormatConditions.Delete

'Setting the criteria for Data Validation
   Set condition = Rng01.FormatConditions.Add(Type:=xlExpression, Formula1:="=$E5<>""Variable""")

'Preferred Data Validation approach that doesn't work
'    With Rng01.Validation
'        .Delete
'        .Add Type:=xlValidateCustom, _
'            AlertStyle:=xlValidAlertStop, _
'            Formula1:="=$E5<>"Variable""
'        .ErrorTitle = "Error Title Here"
'        .ErrorMessage = "Error Message Here"
'    End With
   
    'Conditional Formating to set the background Budget cell color
   With condition
        With Rng01.FormatConditions(1).Interior
            .PatternColorIndex = xlAutomatic
            .ThemeColor = xlThemeColorAccent1
            .TintAndShade = 0.799981688894314
        End With
    End With

'Get cell color
    'Manually Select a cell, choose a color from toolbar, then click out of cell for Color number to display
    'Range("J5:J5") = Range("J5:J5").Interior.color

End Sub

Cell Formulas
RangeFormula
C5,G17:G26,D17:E26,C17,D12:E13,C11:D11,D8:E9,D5:D7,G5:G9,G11:G13C5=JAN!C5
H5:H9,H11:H13,H17:H26H5=G5-F5
F10:H10G10=SUM(G5:G9)
F14:H14F14=SUM(F11:F13)
F15:H15F15=SUM(F14,F10)
F27:H27F27=SUM(F17:F26)
Cells with Conditional Formatting
CellConditionCell FormatStop If True
G5:G9Expression=$E5<>"Variable"textYES
Cells with Data Validation
CellAllowCriteria
E5:E27ListFixed, Variable
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
My understanding is that you need to unprotect the sheet before you can define the formatting.

However, I don't understand why formatting must be changed every time the sheet selection changes (Worksheet_SelectionChange), because conditional formatting automatically reacts to changes in the target area.

I think adding conditional formatting should be done once and without the Worksheet_SelectionChange event.

This is just an example, but it can be modified if conditional formatting can be implemented in your workbook like this?


VBA Code:
Sub AddConditionalFormating()
Worksheets("FEB").Unprotect "Your password here"
'Definining the variables:
  Dim Rng01 As Range
  Dim condition As FormatCondition

'Setting the range on which conditional formatting is to be desired
  Set Rng01 = Range("G5:G9")

'To delete/clear any existing conditional formatting from the range
   Rng01.FormatConditions.Delete

'Setting the criteria for Data Validation
   Set condition = Rng01.FormatConditions.Add(Type:=xlExpression, Formula1:="=$E5<>""Variable""")

'Preferred Data Validation approach that doesn't work
'    With Rng01.Validation
'        .Delete
'        .Add Type:=xlValidateCustom, _
'            AlertStyle:=xlValidAlertStop, _
'            Formula1:="=$E5<>"Variable""
'        .ErrorTitle = "Error Title Here"
'        .ErrorMessage = "Error Message Here"
'    End With
   
    'Conditional Formating to set the background Budget cell color
   With condition
        With Rng01.FormatConditions(1).Interior
            .PatternColorIndex = xlAutomatic
            .ThemeColor = xlThemeColorAccent1
            .TintAndShade = 0.799981688894314
        End With
    End With

'Get cell color
    'Manually Select a cell, choose a color from toolbar, then click out of cell for Color number to display
    'Range("J5:J5") = Range("J5:J5").Interior.color
    Worksheets("FEB").Protect "Your password here"
End Sub



My apologies for any quirks, English is not my native language. "So I blame Google translate for all my goofs." :devilish:
 
Upvote 0
Great that did it thank you.

Would you mind also tackling my other issue mentioned?

1) How to I convert this single line of code
VBA Code:
Set condition = Rng01.FormatConditions.Add(Type:=xlExpression, Formula1:="=$E5<>""Variable""")

Into getting this approach to work?
VBA Code:
'Preferred Data Validation approach that doesn't work
'    With Rng01.Validation
'        .Delete
'        .Add Type:=xlValidateCustom, _
'            AlertStyle:=xlValidAlertStop, _
'            Formula1:="=$E5<>"Variable""
'        .ErrorTitle = "Error Title Here"
'        .ErrorMessage = "Error Message Here"
'    End With

2) How do I get everything to be applied to multiple ranges such as all the Feb-Dec sheets?
 
Upvote 0
Oops I forgot to include these details:

These are the ranges:
Range("G5:G9,G11:G17,G21:G32,G34:G41,G43:G49,G51:G63,G65:G70,G72:G82,G86:G93")

These are the formula values for each respective range:
=$E5<>"Variable"
=$E11<>"Variable"
=$E21<>"Variable"
=$E34<>"Variable"
=$E43<>"Variable"
=$E51<>"Variable"
=$E65<>"Variable"
=$E72<>"Variable"
=$E86<>"Variable"
 
Upvote 0
I have a small doubt that your intention is not to add to the range Range("G5:G9") Data Validation with the rule Formula1:="=$E5<>"Variable""

Formula1:="=$E5<>"Variable"" means that all other input to the cell is allowed except the text "Variable"

What do you want Data Validation to do?
Maybe it's a list to choose Fixed or Variable?
Or something else?
 
Upvote 0
Yes it is based on the drop down list in each respective section as demo'd in the video.

Sorry, here's the correct list, the other was for Conditional Formatting I was doing:
=$E5="Variable"
=$E11="Variable"
=$E21="Variable"
=$E34="Variable"
=$E43="Variable"
=$E51="Variable"
=$E65="Variable"
=$E72="Variable"
=$E86="Variable"
 
Upvote 0

Forum statistics

Threads
1,224,881
Messages
6,181,539
Members
453,054
Latest member
ezzat

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