VBA and dropdown list: change background color relating the user's choice

Nelson78

Well-known Member
Joined
Sep 11, 2017
Messages
526
Office Version
  1. 2007
Hello everybody.

I've set a dropdown list this way:


Code:
Dim MyList(2) As String
MyList(0) = "YES'"
MyList(1) = "NO"

Dim lrut As Long
    lrut = sheet1.Cells(Rows.Count, "E").End(xlUp).Row

With Range("G2:G" & lrut).Validation
    .Delete
    .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
         Operator:=xlBetween, Formula1:=Join(MyList, ",")
End With

I need a change in the color background of the cell in case of choice "YES".

How can I manage the matter?
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Try:
Code:
Sub tetingcopy()
    Dim MyList(2) As String
    MyList(0) = "YES"
    MyList(1) = "NO"
    Dim lrut As Long
        lrut = Sheet1.Cells(Rows.Count, "E").End(xlUp).Row
    With Range("G2:G" & lrut).Validation
        .Delete
        .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
             Operator:=xlBetween, Formula1:=Join(MyList, ",")
    End With
    Range("G2:G" & lrut).Select
    Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, Formula1:="=""YES"""
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    With Selection.FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .Color = 255
        .TintAndShade = 0
    End With
    Selection.FormatConditions(1).StopIfTrue = False
End Sub
 
Last edited:
Upvote 0
Try:
Code:
Sub tetingcopy()
    Dim MyList(2) As String
    MyList(0) = "YES"
    MyList(1) = "NO"
    Dim lrut As Long
        lrut = Sheet1.Cells(Rows.Count, "E").End(xlUp).Row
    With Range("G2:G" & lrut).Validation
        .Delete
        .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
             Operator:=xlBetween, Formula1:=Join(MyList, ",")
    End With
    Range("G2:G" & lrut).Select
    Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, Formula1:="=""YES"""
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    With Selection.FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .Color = 255
        .TintAndShade = 0
    End With
    Selection.FormatConditions(1).StopIfTrue = False
End Sub

Yes, it works absolutely fine.

Anyway, I'm a bit perplexed by the use of
Code:
Select
and
Code:
Selection
, that usually I avoid.

What do you think about?
 
Upvote 0
Generally, you don't have to select a range to perform an action on it. I took the lazy way out and recorded a macro to do the conditional formatting. This revised macro does the same thing without the use of "Select".
Code:
Sub tetingcopy()
    Dim MyList(2) As String
    MyList(0) = "YES"
    MyList(1) = "NO"
    Dim lrut As Long
        lrut = Sheet1.Cells(Rows.Count, "E").End(xlUp).Row
    With Range("G2:G" & lrut).Validation
        .Delete
        .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
             Operator:=xlBetween, Formula1:=Join(MyList, ",")
    End With
    With Range("G2:G" & lrut)
        .FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, Formula1:="=""YES"""
        .FormatConditions(Range("G2:G" & lrut).FormatConditions.Count).SetFirstPriority
    End With
    With Range("G2:G" & lrut).FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .Color = 255
        .TintAndShade = 0
    End With
    Range("G2:G" & lrut).FormatConditions(1).StopIfTrue = False
End Sub
 
Upvote 0
Generally, you don't have to select a range to perform an action on it. I took the lazy way out and recorded a macro to do the conditional formatting. This revised macro does the same thing without the use of "Select".
Code:
Sub tetingcopy()
    Dim MyList(2) As String
    MyList(0) = "YES"
    MyList(1) = "NO"
    Dim lrut As Long
        lrut = Sheet1.Cells(Rows.Count, "E").End(xlUp).Row
    With Range("G2:G" & lrut).Validation
        .Delete
        .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
             Operator:=xlBetween, Formula1:=Join(MyList, ",")
    End With
    With Range("G2:G" & lrut)
        .FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, Formula1:="=""YES"""
        .FormatConditions(Range("G2:G" & lrut).FormatConditions.Count).SetFirstPriority
    End With
    With Range("G2:G" & lrut).FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .Color = 255
        .TintAndShade = 0
    End With
    Range("G2:G" & lrut).FormatConditions(1).StopIfTrue = False
End Sub

Well, the same tweak I'd have done.

Thank's a lot.
 
Upvote 0

Forum statistics

Threads
1,223,894
Messages
6,175,254
Members
452,623
Latest member
Techenthusiast

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