Merge to codes under Private Sub Worksheet_Change event

Dokat

Active Member
Joined
Jan 19, 2015
Messages
304
Office Version
  1. 365
Hi,

I have two codes that i use to

1. Reset dependent dropdown list if value in A6 changes
2. change the cell color based on the value in the cell D6 changes.

Here are the codes i am using. Is there anyway to combine them under one code so that when values change it automatically updates.

Code to reset dependent list.

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

    Application.EnableEvents = False
    
    If Not Intersect(Target, Target.Worksheet.Range("A6")) Is Nothing Then
        Target.Offset(0, 1).Value = ""
        Target.Offset(0, 2).Value = ""
    End If
    Application.EnableEvents = True

End Sub

Code to change cell color based on value

Code:
Sub Color_Change
Application.EnableEvents = False

Dim MyCell As Range
Dim StatValue As String
Dim StatusRange As Range

Set StatusRange = Range("D6")

For Each MyCell In StatusRange

StatValue = MyCell.Value
Select Case StatValue

    Case "Low"
    MyCell.Interior.Color = RGB(0, 176, 80)
    
    Case "Moderate"
    MyCell.Interior.Color = RGB(255, 230, 153)
    
    Case "High"
    MyCell.Interior.Color = RGB(255, 204, 204)

End Select

Next MyCell

End Sub
 
This could be another solution:
VBA Code:
Sub Color_Change()
    On Error GoTo xit
    Select Case Range("D6")
        Case "Low"
            Range("D6").Interior.Color = RGB(0, 176, 80)
        Case "Moderate"
            Range("D6").Interior.Color = RGB(255, 230, 153)
        Case "High"
            Range("D6").Interior.Color = RGB(226, 239, 218)
    End Select
    Exit Sub
xit:
    Range("D6").Interior.Color = RGB(255, 0, 0)
End Sub
 
Upvote 0

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
This could be another solution:
VBA Code:
Sub Color_Change()
    On Error GoTo xit
    Select Case Range("D6")
        Case "Low"
            Range("D6").Interior.Color = RGB(0, 176, 80)
        Case "Moderate"
            Range("D6").Interior.Color = RGB(255, 230, 153)
        Case "High"
            Range("D6").Interior.Color = RGB(226, 239, 218)
    End Select
    Exit Sub
xit:
    Range("D6").Interior.Color = RGB(255, 0, 0)
End Sub
This one worked with the call function
 
Upvote 0

Forum statistics

Threads
1,215,036
Messages
6,122,796
Members
449,095
Latest member
m_smith_solihull

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