Excel data validation: Change fill color without changing text

你离开我真会死。 提交于 2020-01-06 09:37:33

问题


I have a floor plan with about 50 resources that either need to be marked available, reserved, or occupied. I chose to use data validation in-cell drop-down list for each resource. Is it possible to keep the text in the data validation cell, use the source as the drop-down options, and change the fill color based on the selection? That might be hard to understand so I'll provide an example.

Example given:

A1:A3 is the source with Available, Reserved, and Occupied.

Cell D3: Available, Reserved, and Occupied as drop-down list, the fill color will change to green, orange, or red respectively, and it will keep the text in the cell.


回答1:


First, use Data Validation to create the list in based on the values in cells A1:A3 (Available, Reserved, and Occupied) in cell D3.

Then, use Conditional Formatting to setup three rules through "Use a formula to determine which cells to format". You'll need one for each colour, ie.

=$D$3="Available" - Applies to: =$D$3 - Format: Green fill
=$D$3="Reserved" - Applies to: =$D$3 - Format: Orange fill
=$D$3="Occupied" - Applies to: =$D$3 - Format: Red fill



回答2:


You can use Conditional Formatting to achieve the same. Example given below is using MS-Excel 2007.

Assuming that you have a dropdown already present at D3 (or multiple cells), select the cell (or cells) in which the formatting is required.

In Home tab -> go to Conditional Formatting option -> select Highlight Cell Rules -> Equal To option. A dialog box will open as shown in the image below. Enter the cell reference that you want and the format required (e.g. $A$1 for Available) and click OK.

Repeat the same for Reserved and Occupied as well with appropriate formatting.




回答3:


First place the ComboBox control and set the .visible property to false. using .AddItem method add Available and others. to its list. I think you want something like below.

Private mrngMyRange As Range

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    If Target.Column = 4 Then 'This restricts the sensitive range to column `D`
        ComboBox1.Visible = True
        ComboBox1.Left = Target.Left
        ComboBox1.Top = Target.Top
        Set mrngMyRange = Target
        ComboBox1.Select
    Else
        ComboBox1.Visible = False
    End If
End Sub

Private Sub ComboBox1_Change()
    'mrngMyRange.Value = ComboBox1.Text

    If ComboBox1.Text = "Available" Then
        mrngMyRange.Cells(1, 1).Interior.ColorIndex = 4
    ElseIf ComboBox1.Text = "Reserved" Then
        mrngMyRange.Cells(1, 1).Interior.ColorIndex = 46
    ElseIf ComboBox1.Text = "Occupied" Then
        mrngMyRange.Cells(1, 1).Interior.ColorIndex = 3
    End If

    ComboBox1.Visible = False
End Sub


来源:https://stackoverflow.com/questions/29465932/excel-data-validation-change-fill-color-without-changing-text

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!