1

我想在下面的代码中给出一个范围而不是偏移量。以下代码将颜色从范围(C5:F11)复制到我设置的偏移量,但我想指定一个范围,如范围(M5:P11)。我试过它只是用范围替换偏移量,但它不能正常工作。请帮忙

Sub MatchColors2()

For Each myCellColor In Range("C5:F11") 
    myCellColor.Offset(0, 8).Interior.ColorIndex = myCellColor.Interior.ColorIndex
Next
End Sub

谢谢

4

1 回答 1

3

根据您的要求,这也适用于具有多个区域的范围,即非连续范围:

Sub MatchColors2()
Dim rngTo As Excel.Range
Dim rngFrom As Excel.Range
Dim i As Long
Dim j As Long

Set rngTo = ActiveSheet.Range("C5:D11,F5:G11")
Set rngFrom = ActiveSheet.Range("I5:J11,L5:M11")

For i = 1 To rngFrom.Areas.Count
    For j = 1 To rngFrom.Areas(i).Cells.Count
        rngTo.Areas(i).Cells(j).Interior.ColorIndex = rngFrom.Areas(i).Cells(j).Interior.ColorIndex
    Next j
Next i
End Sub
于 2013-10-15T19:29:48.230 回答