簡體   English   中英

基於單元格值的條件格式形狀填充

[英]Conditional Format Shape Fill Based on Cell Value

我不想問這個問題,因為我不知道從哪里開始,所以我現在沒有任何代碼。 我看過一些關於這個主題的東西,但找不到我要找的東西。

表格是 5 列(ID + 螺栓數)x 13 行(ID)

我有四種形狀 (Oval4-Oval7),我想根據四個相應的單元格從紅色/橙色/綠色更改(這些單元格值的選項是:空、已安裝、已扭曲)。

這些形狀還會根據第一列中選擇的 ID (1-13) 更改顏色。

因此,如果您將 cursor 放在 ID 2 單元格上,形狀將根據同一行第 2-5 列中的值更改顏色。

這太復雜了嗎?

我會自己繼續努力。 只是想我會從這里開始。

謝謝你的時間。

下面的代碼有效,但我如何將它應用於整個表格?

 Private Sub Worksheet_Change(ByVal Target As Range)
 If Range("d12") = "Empty" Then
 ActiveSheet.Shapes.Range(Array("Shape1")).Select
 Selection.ShapeRange.Fill.ForeColor.RGB = RGB(255, 0, 0)
 Else
 If Range("d12") = "Installed" Then
 ActiveSheet.Shapes.Range(Array("Shape1")).Select
 Selection.ShapeRange.Fill.ForeColor.RGB = RGB(255, 155, 0)
 Else
 If Range("d12") = "Torqued" Then
 ActiveSheet.Shapes.Range(Array("Shape1")).Select
 Selection.ShapeRange.Fill.ForeColor.RGB = RGB(0, 255, 0)
 End If
 End If
 End If
 End Sub

在此處輸入圖像描述

在工作表代碼模塊中:

Private Sub Worksheet_Change(ByVal Target As Range)
    ResolveSelection Target.Cells(1)
End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    ResolveSelection Target.Cells(1)
End Sub

'Is the selected/changed cell in one of the two tables?
'  If Yes get the full row for that cell and pass to SetRow
Sub ResolveSelection(Target As Range)
    Dim r, rng As Range
    For Each r In Array("B3:G14", "J3:O14") 'my 2 test tables
        Set rng = Application.Intersect(Target, Me.Range(r))
        If Not rng Is Nothing Then
            'get the whole row of the table
            Set rng = Application.Intersect(Target.EntireRow, Me.Range(r))
            SetRow rng
            Exit Sub
        End If
    Next r
End Sub

'set the coloring based on the row 'rw'
Sub SetRow(rw As Range)
    Dim i As Long, shp As Shape
    Debug.Print rw.Address
    For i = 1 To 4
        Set shp = rw.Parent.Shapes("Shape" & i)
        shp.Fill.ForeColor.RGB = GetColor(rw.Cells(2 + i).Value)
    Next i
End Sub

'get the color for a given state
Function GetColor(v As String) As Long
    Select Case v & ""
        Case "Empty", "": GetColor = vbRed
        Case "Installed": GetColor = RGB(255, 155, 0)
        Case "Torqued": GetColor = vbGreen
        Case Else: GetColor = vbWhite
    End Select
End Function

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM