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
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Add Call Color_Change just after Application.EnableEvents = True.
Every time you change any cell in the sheet the event macro will launch your color change macro which will check cell D6.
If you need to change color only when you change A6 then insert the Call Color_Change just before the End If instead of after Application.EnableEvents = True.
 
Upvote 0
Add Call Color_Change just after Application.EnableEvents = True.
Every time you change any cell in the sheet the event macro will launch your color change macro which will check cell D6.
If you need to change color only when you change A6 then insert the Call Color_Change just before the End If instead of after Application.EnableEvents = True.
Thank you for your response. Per your recommendation i modified the code however it didnt work. Please see below updated code. It gives type mismatch error.

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
    Call Color_Change
End Sub

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(226, 239, 218)

End Select

Next MyCell

End Sub
 
Upvote 0
May I ask ? is your color macro in working condition ? because the Call statement only lauches a macro (it doesn't fix non working macros :rolleyes:).
By the way, where does it throw the error ? which line. And please remember, I have no idea about your project.
 
Upvote 0
May I ask ? is your color macro in working condition ? because the Call statement only lauches a macro (it doesn't fix non working macros :rolleyes:).
Yes, it will change color based on the selection value in cell D6.

Thanks
 
Upvote 0
Before your last post I edited mine so please reply to the additional question. As said Call only launches the macro so please show the contents of cells A6 and D6 (formulas, validations or whatever).
Also, your macro has redunctant code, you could cut it down to:
VBA Code:
Sub Color_Change()
    Select Case Range("D6").Value
        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
End Sub
 
Upvote 0
Thanks below is the formula in D6. There are no formulas in A6, It is just a dropdown list. code allows me to reset dependent drop down list when my selection in a6 changes.

Content in D6
1668189351452.png
 
Upvote 0
Sorry to say that your macro Change_Color isn't in working conditional when the item in A6 matched with B6 and C6 isn't found in your column U of list U5:Z57.
It will throw error 13 mismatch on line Case "Low".
 
Upvote 0
Sorry to say that your macro Change_Color isn't in working conditional when the item in A6 matched with B6 and C6 isn't found in your column U of list U5:Z57.
It will throw error 13 mismatch on line Case "Low".
Thanks. Is there a work aount?
 
Upvote 0
The quickest way is to add On Error Resume Next at begining of the color macro but you will have to accept a #N/D with green background in cell D6.
 
Upvote 0

Forum statistics

Threads
1,214,918
Messages
6,122,243
Members
449,075
Latest member
staticfluids

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