[英]Change colour of selected cell using values in cells based off the selected cell
我有一個宏可以根據 RGB 值為單元格着色。
我有 RGB 值的單元格 A1、B1 和 C1。
Sub FillWithRBG()
Range("D1").Interior.Color = RGB(Range("A1").Value, Range("B1").Value, Range("C1").Value)
End Sub
我可以選擇工作表中的任何地方,運行宏,只有單元格 D1 會改變顏色。
我想選擇單元格 D2,運行宏,然后單元格 D2 根據 A2、B2 和 C2 中的值更改顏色。
我想我需要使用引用設置活動單元格,然后 1、2 和 3 將從所選單元格偏移。
一個額外的好處是宏只能在 D 列中運行以防止錯誤。
這將是一個簡單的方法
在代碼Me.
指的是當前工作表
您必須將此代碼放在工作表的模塊中
代碼:
Private Sub Worksheet_Change(ByVal Target As Range)
' Prevent change if changed values are no in these columns (1,2,3) numbers are equivalent to A, B, C
If Target.Column > 3 Then Exit Sub
' Change color of D column (4 = D)
Me.Cells(Target.Row, 4).Interior.Color = RGB(Me.Cells(Target.Row, 1).Value, Me.Cells(Target.Row, 2).Value, Me.Cells(Target.Row, 3).Value)
End Sub
如果您希望它僅在一次更改一個單元格時工作,請添加以下行:
' Prevent change when more than one cell is changed If Target.Cells.Count > 1 Then Exit Sub
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.