简体   繁体   中英

How to copy cell colour defined by conditional formatting to another cell

Please help me, as I am not very experienced with VBA.

Let's say I have the following cells with specific background colour as defined by conditional formatting. (Cells A3:A6)

在此处输入图像描述

Now I would like cells (C3:C6) to copy the colour from A3:A6 and apply it to C3:C6.

在此处输入图像描述

Please help me get a code that will recognize the colour set from Conditional formatting and change C3:C6 respectively. Thank you very much.

EDIT:I messed up and didn't enter any values for cells so it may be confusing how is there conditional formatting when nothing is in the cell. Assume cells with colours are 1-5.

Current code:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

Dim xSRg, xDRg, xISRg, xIDRg As Range
Dim xFNum As Long
On Error Resume Next
Set xSRg = Sheet1.Range("A3:A5")
Set xDRg = Sheet1.Range("C3:C6")
For xFNum = 1 To xSRg.Count
Set xISRg = xSRg.Item(xFNum)
Set xIDRg = xDRg.Item(xFNum)
xIDRg.DisplayFormat.Interior.Color = xISRg.DisplayFormat.Interior.Color
Next xFNum
End Sub

To be honest - your question/your requirement doesn't make any sense in my eyes (esp. within the selection change event and w/o using target) - but here is the code:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

With Sheet1
    Dim rgFormatCondition As Range
    Set rgFormatCondition = Me.Range("A3:A6")
    
    Dim rgTarget As Range
    Set rgTarget = Me.Range("C3:C6")
    
    Dim i As Long
    For i = 1 To rgFormatCondition.Rows.Count
        rgTarget(i).Interior.color = rgFormatCondition(i).DisplayFormat.Interior.color
    Next
End With
End Sub

You have to apply the displayformat-color of the cells with format condition to the interior color of the target cell.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM