Conditional Formatting vba + Reverting the Conditional Formatting if no data

leila

New Member
Joined
Aug 17, 2007
Messages
6
I have this code that it is working except the reverting to original format. How I can do that?

Private Sub Worksheet_Change(ByVal Target As Range)
Dim rngCheck As Range
Dim cell As Range
Dim rngOriginalFormat As Range
Dim rngWhiteCells As Range
Dim rngGrayCells As Range
Dim rngGreenFont As Range

' Define the range to check for changes in column D
Set rngCheck = Intersect(Target, Me.Columns("D"))

' Exit if no change in column D or multiple cells were changed
If rngCheck Is Nothing Or rngCheck.Count > 1 Then Exit Sub

Application.ScreenUpdating = False

' Loop through each cell in the changed range
For Each cell In rngCheck
' Check if the cell is in one of the specified rows
Select Case cell.row
Case 25
Set rngOriginalFormat = Union(Range("A25:I26"), Range("E25:I32"))
Set rngWhiteCells = Union(Range("E25:I26"), Range("E28:I29"), Range("E31:I32"))
Set rngGrayCells = Union(Range("F27"), Range("F30"))
Set rngGreenFont = Range("F25")

Case 36
Set rngOriginalFormat = Union(Range("A36:D37"), Range("E38:I43"))

Case 44
Set rngOriginalFormat = Union(Range("A44:D45"), Range("E46:I51"))

End Select

' Apply or revert formatting based on the presence of data in column D
If Not IsEmpty(cell) Then
' Apply formatting when data is entered
With rngOriginalFormat
.Font.Color = RGB(128, 128, 128)
.Interior.Color = RGB(214, 217, 219)
End With

Else
' Revert to original formatting when data is removed
With rngOriginalFormat
.Font.Color = vbBlack
.Interior.ColorIndex = xlNone
End With
rngWhiteCells.Interior.Color = RGB(255, 255, 255)
rngGrayCells.Interior.Color = RGB(214, 217, 219)
rngGreenFont.Font.Color = RGB(115, 134, 25)
End If

Next cell

Application.ScreenUpdating = True
End Sub
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

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