![](/img/trans.png)
[英]I want to compare 3 different columns (B,C,D) values then want to get the higher value result in F column
[英]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。
不幸的是,您沒有透露您詢問的代碼的邏輯目的。 因此我發明了一個。 如果它不適合您的需求,您可以根據您的需求調整代碼的功能。 首先,嘗試一下。
在空白工作表的代碼模塊中安裝以下代碼。 代碼位於該特定模塊中很重要。 它在 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 列中:
然后,
在 D 列中,select D1:D7
>> 條件格式 >> 新規則 >>
=LOOKUP(2,1/($A1:$C1<>""),ROW($A$1:$A$3))=1
>> 格式 >> “ Red
”背景顏色 >> OK=LOOKUP(2,1/($A1:$C1<>""),ROW($A$1:$A$3))=2
>> 格式 >> “ Green
”背景顏色 >> OK 規則 3 公式: =LOOKUP(2,1/($A1:$C1<>""),ROW($A$1:$A$3))=3
>> 格式 >> “ Blue
”背景顏色 >> OK
結束
D列彩色單元格結果:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.