Undoing VBA color changes of a cell having Conditional Formatting

Bwanna

New Member
Joined
Jan 10, 2018
Messages
13
Hi and thank you ahead of time for helping me figure this out.

Situation:
-cell A1 has CF that imposes a gray fill color is the cell is BLANK
-Need to have VBA code that changes that color to Black based on certain conditions

Dilemma:
-When the VBA conditions are no longer, how do I get the CF to be reapplied for cell A1

Currently trying:
Code to Change Color of Cell:
-Range (A1).Interior.ColorIndex = 1
-Range (A1).FormatConditions.Delete

Code to Undo the VBA Color changing of Cell:
-Range (A1).Interior.ColorIndex = xlNone


-IS there a way to manually recycle the CF rules? i.e. reapply

I also noticed that after the VBA code "xlNone" is applied, NONE of the CF rules apply to cell A1 as a result of user entering new values into the cell (other CF rules apply different colors depending on values)

Thanks again,
 
Last edited:

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
I'm guessing this will return it to normal. Maybe stop using the xlNone thing and the CF will work again? Dave
Code:
Range (A1).Interior.ColorIndex =  xlAutomatic
 
Upvote 0
The original information was to simplify the request, but here's the planned code in case it helps explain

Code:
Private Sub Worksheet_Change(ByVal Target As Range)If Target.Cells.Count > 1 Then Exit Sub
    If Not Intersect(Target, Range("I6:I14")) Is Nothing And Target.Cells.Count = 1 Then
        ActiveSheet.Unprotect
            Dim i As Integer
            If Target.Value = "PASSTHRU" Then
                For i = 1 To 5
                    Target.Offset(0, i).Interior.ColorIndex = 1
                    Target.Offset(0, i).FormatConditions.Delete
                    Target.Offset(0, i).Locked = True
                Next i
            Else
                For i = 1 To 5
                    Target.Offset(0, i).Interior.ColorIndex = xlNone
                    Target.Offset(0, i).Locked = False
                Next i
            End If
        ActiveSheet.Protect
    End If
End Sub

The cell conditions in the Range (I6:I14) effect the 5 cells to the right (Fill BLACK). It's in undoing this that I'm having the challenge, either by selecting a different dropdown or manually deleting the contents of the cell... I can't seem to get the CF's to retake. Even when I select a dropdown in THOSE cells, the CF doesn't take.
 
Upvote 0
Can't get the sheet protection to work but it seems like the rest works. Give it a trial. Dave
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Cells.Count > 1 Then Exit Sub
    If Not Intersect(Target, Range("I6:I14")) Is Nothing And Target.Cells.Count = 1 Then
    'ActiveSheet.Unprotect
            Dim i As Integer
            If Target.Value = "PASSTHRU" Then
            
                For i = 1 To 5
                    Target.Offset(0, i).Interior.ColorIndex = 1
                    Target.Offset(0, i).FormatConditions.Delete
                    Target.Offset(0, i).Locked = True
                Next i
            Else
                For i = 0 To 5
                    Target.Offset(0, i).Interior.ColorIndex = xlColorIndexNone
                    Target.Offset(0, i).Locked = False
                Next i
            End If
    'ActiveSheet.Protect
    End If
End Sub
 
Upvote 0
Can't get the sheet protection to work but it seems like the rest works. Give it a trial. Dave
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Cells.Count > 1 Then Exit Sub
    If Not Intersect(Target, Range("I6:I14")) Is Nothing And Target.Cells.Count = 1 Then
    'ActiveSheet.Unprotect
            Dim i As Integer
            If Target.Value = "PASSTHRU" Then
            
                For i = 1 To 5
                    Target.Offset(0, i).Interior.ColorIndex = 1
                    Target.Offset(0, i).FormatConditions.Delete
                    Target.Offset(0, i).Locked = True
                Next i
            Else
                For i = 0 To 5
                    Target.Offset(0, i).Interior.ColorIndex = xlColorIndexNone
                    Target.Offset(0, i).Locked = False
                Next i
            End If
    'ActiveSheet.Protect
    End If
End Sub

THANKS, I'll try this later, for now I separated out all the Cell Fill formatting to CF and the Locking to VBA.
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,287
Members
452,631
Latest member
a_potato

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