【问题标题】:How to specify range instead of offset , excel VBA?如何指定范围而不是偏移量,excel VBA?
【发布时间】:2013-10-15 19:11:35
【问题描述】:

我想在下面的代码中给出一个范围而不是偏移量。以下代码将颜色从范围(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

谢谢

【问题讨论】:

    标签: excel vba colors


    【解决方案1】:

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

    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
    

    【讨论】:

    • @SiddharthRout,这是一个罕见的事件!
    • @ShanS,请点击旁边的复选标记,接受此内容以及对其他问题的任何正确答案。
    • 但老实说..你付出了额外的努力来使它在不连续的范围内工作......我的代码没有这样做......感谢上帝你打败了我......:p如果我给出了一个低于标准的答案,那将是非常可耻的......
    猜你喜欢
    • 2017-10-18
    • 1970-01-01
    • 2013-07-21
    • 2016-05-02
    • 1970-01-01
    • 1970-01-01
    • 2018-10-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多