簡體   English   中英

我有一個 Excel 表,它有 3 列 AB C 列,我希望 D 列是 RGB(A,B,C) 的顏色

[英]I have an Excel sheet that has 3 columns A B C columns and I want the D column to be the color of RGB(A,B,C)

我正在使用 Excel 2019/365 32 位。

I have a sheet with many rows and 3 columns Column A is the Red component (an integer from 0 - 255) Column B is the Green component (an integer from 0 - 255) Column C is the Blue component (an integer from 0 - 255) 在顏色公式 RGB(A,B,C) 中,我希望 D 列是該行的 RGB(A,B,C) 產生的顏色 如何為 D 列分配顏色?

RBG(A,B,C) A, B, C 可以是從 0 到 255 的任何 integer 例如 RGB(255,0,0) = 顏色紅色,黃色是 RGB(255(.255,0),綠色 = RGB 0,255,0),藍色 = RGB(0,0,255)

條件格式是不可能的,因為 colors 的可能組合超過 500 個可能的 colors。 所以我需要一個 function 或 VBA 代碼來用響應 RGB() 的顏色填充 D。

我想要的一個例子,你看到的 colors 不是條件格式計算的

不幸的是,您沒有透露您詢問的代碼的邏輯目的。 因此我發明了一個。 如果它不適合您的需求,您可以根據您的需求調整代碼的功能。 首先,嘗試一下。

在空白工作表的代碼模塊中安裝以下代碼。 代碼位於該特定模塊中很重要。 它在 VB 編輯器的項目資源管理器中通過其名稱進行標識。

Private Sub Worksheet_Change(ByVal Target As Range)

    ' identify the columns containing R, B & G
    Const TriggerAddress As String = "A:C"

    Dim Rng As Range
    Dim Arr As Variant

    If Not Application.Intersect(Target, Range(TriggerAddress)) Is Nothing Then
        With Target
            ' ignore changes by Paste action
            If .Cells.CountLarge = 1 Then
                Set Rng = Range(TriggerAddress).Rows(.Row)
                ' require 3 numbers
                If Application.Count(Rng) = 3 Then
                    Arr = Rng.Value
                    Cells(.Row, Rng.Cells.Count + 1).Interior.Color = _
                    RGB(Arr(1, 1), Arr(1, 2), Arr(1, 3))
                End If
            End If
        End With
    End If
End Sub

該過程頂部的常量標識了您擁有數字的列。 您要求 A:C 現在是這樣,但將來可以更改。 請注意,output 將按照您的要求位於水平相鄰的單元格中,但這也可以更改。

TriggerColumns中的任何單元格發生更改時,代碼將做出反應。 如果所有 3 個單元格都包含數字,則 output 單元格的填充顏色將被更改。 在這種情況下,0(零)並不是一個“數字”。 修改 3 個現有數字中的任何一個,顏色將再次重置。

實際答案是:

For x = StartRowNumber To EndRowNumber
    Cells(x, 4).Interior.Color = RGB(Cells(x, 1), Cells(x, 2), Cells(x, 3))
Next

使用條件格式方式

單元格顏色規則出現在 A 列、B 列和 C 列中:

  • 紅色:僅 A 列
  • 綠色:僅 B 列,A 列 + B 列
  • Blue Color: Column C only, Column A + Column C, Column B + Column C, Column A + Column B + Column C

然后,

在 D 列中,select D1:D7 >> 條件格式 >> 新規則 >>

  1. 規則1公式: =LOOKUP(2,1/($A1:$C1<>""),ROW($A$1:$A$3))=1 >> 格式 >> “ Red ”背景顏色 >> OK
  2. 規則2公式: =LOOKUP(2,1/($A1:$C1<>""),ROW($A$1:$A$3))=2 >> 格式 >> “ Green ”背景顏色 >> OK
  3. 規則 3 公式: =LOOKUP(2,1/($A1:$C1<>""),ROW($A$1:$A$3))=3 >> 格式 >> “ Blue ”背景顏色 >> OK

  4. 結束

在此處輸入圖像描述

D列彩色單元格結果:

在此處輸入圖像描述

暫無
暫無

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

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